[LintCode] Distinct Subsequence

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Example

Given S = "rabbbit", T = "rabbit", return 3.

Challenge 

Do it in O(n2) time and O(n) memory.

O(n2) memory is also acceptable if you do not know how to optimize memory.

 

Solution 1. Recursion

For s[0 ~ n - 1] and t[0 ~ m - 1], there are 2 cases.

case 1. s.charAt(n - 1) != t.charAt(m - 1)

     This means s.charAt(n - 1) can't be used in subsequence of t, so f(s[0 ~ n - 1], t[0 ~ m - 1]) = f(s[0 ~ n - 2], t[0 ~ m - 1]).

case 2. s.charAt(n - 1) == t.charAt(m - 1)

      This means s.charAt(n - 1) can be either used or not used in subsequence of t.

    If it is used, there are f(s[0 ~ n - 2], t[0 ~ m - 2]) distinct subsequences of t in s.

    If it is not used, there are f(s[0 ~ n - 2], t[0 ~ m - 1]) distinct subsequences of t in s.

    so f(s[0 ~ n - 1], t[0 ~ m - 1]) = f(s[0 ~ n - 2], t[0 ~ m - 2]) + f(s[0 ~ n - 2], t[0 ~ m - 1]).

Base case 1.  if t is an empty string, then there is 1 distinct subseqnce of empty string in s.

Base case 2.  if t is longer than s, there is no subsequences of t in s.

It's clear by drawing recursive tree, that this solution has overlapping problems.

 1 public class DistinctSubsequence {
 2     public int getNumOfDistinctSubsequenceRecursion(String s, String t) {
 3         if(s == null || t == null) {
 4             return 0;
 5         }
 6         return recursiveHelper(s, s.length() - 1, t, t.length() - 1);
 7     }
 8     private int recursiveHelper(String s, int endIdx1, String t, int endIdx2) {
 9         if(endIdx2 < 0) {
10             return 1;
11         }
12         if(endIdx1 < endIdx2) {
13             return 0;
14         }
15         if(s.charAt(endIdx1) != t.charAt(endIdx2)) {
16             return recursiveHelper(s, endIdx1 - 1, t, endIdx2);
17         }
18         return recursiveHelper(s, endIdx1 - 1, t, endIdx2 - 1) + recursiveHelper(s, endIdx1 - 1, t, endIdx2);
19     }
20     public static void main(String[] args) {
21         String s1 = "rabbbit", t1 = "rabbit";
22         String s2 = "aabbccdd", t2 = "abcd";
23         String s3 = "rabbbitt", t3 = "rabbit";        
24         String s4 = "", t4 = "";
25         String s5 = "lin", t5 = "lin";
26         String s6 = "lin", t6 = "ling";
27         DistinctSubsequence test = new DistinctSubsequence();
28         System.out.println(test.getNumOfDistinctSubsequenceRecursion(s1, t1));
29         System.out.println(test.getNumOfDistinctSubsequenceRecursion(s2, t2));
30         System.out.println(test.getNumOfDistinctSubsequenceRecursion(s3, t3));
31         System.out.println(test.getNumOfDistinctSubsequenceRecursion(s4, t4));
32         System.out.println(test.getNumOfDistinctSubsequenceRecursion(s5, t5));
33         System.out.println(test.getNumOfDistinctSubsequenceRecursion(s6, t6));
34     }
35 }

 

Solution 2. Dynamic Programming

State: dp[i][j]: the number of distinct subsequence of T[0... j - 1] in S[0... i - 1]

Function:  

If S.charAt(i - 1) can be used to match the last character of T[0...j - 1],  we can either choose to use it or not use it.

If S.charAt(i - 1) can't be used to match the last character of T[0...j - 1], we can only choose not to use it.

The above gives us the following state function.

dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j], if S.charAt(i - 1) == T.charAt(j - 1); 

dp[i][j] = dp[i - 1][j], if S.charAt(i - 1) != T.charAt(j - 1); 

Initialization: dp[0][j] = 0, for j from 1 to T.length(); S is empty string and T is not empty string, no subsequence of T in S;

      dp[i][0] = 1, for i from 0 to S.length(); T is empty string, there is always 1 subsequence of T in S, regardless if S is empty or not.

Answer: dp[S.length()][T.length()]

 

 1 public class Solution {
 2     public int numDistinct(String S, String T) {
 3         if(S == null || T == null){
 4             return 0;
 5         }
 6         int n = S.length();
 7         int m = T.length();
 8         int[][] dp = new int[n + 1][m + 1];
 9         for(int j = 0; j <= m; j++){
10             dp[0][j] = 0;
11         }
12         for(int i = 0; i <= n; i++){
13             dp[i][0] = 1;
14         }
15         for(int j = 1; j <= m; j++){
16             for(int i = j; i <= n; i++){
17                 if(S.charAt(i - 1) == T.charAt(j - 1)){
18                     dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
19                 }
20                 else{
21                     dp[i][j] = dp[i - 1][j];
22                 }
23             }
24         }
25         return dp[n][m];
26     }
27 }

 

 

Related Problems 

Interleaving String

转载于:https://www.cnblogs.com/lz87/p/7416795.html

演示了为无线无人机电池充电设计的感应电力传输(IPT)系统 Dynamic Wireless Charging for (UAV) using Inductive Coupling 模拟了为无人机(UAV)量身定制的无线电力传输(WPT)系统。该模型演示了直流电到高频交流电的转换,通过磁共振在气隙中无线传输能量,以及整流回直流电用于电池充电。 系统拓扑包括: 输入级:使用IGBT/二极管开关连接到全桥逆变器的直流电压源(12V)。 开关控制:脉冲发生器以85 kHz(周期:1/85000秒)的开关频率运行,这是SAE J2954无线充电标准的标准频率。 耦合级:使用互感和线性变压器块来模拟具有特定耦合系数的发射(Tx)和接收(Rx)线圈。 补偿:包括串联RLC分支,用于模拟谐振补偿网络(将线圈调谐到谐振频率)。 输出级:桥式整流器(基于二极管),用于将高频交流电转换回直流电,以供负载使用。 仪器:使用示波器块进行全面的电压和电流测量,用于分析输入/输出波形和效率。 模拟详细信息: 求解器:离散Tustin/向后Euler(通过powergui)。 采样时间:50e-6秒。 4.主要特点 高频逆变:模拟85 kHz下IGBT的开关瞬态。 磁耦合:模拟无人机着陆垫和机载接收器之间的松耦合行为。 Power GUI集成:用于专用电力系统离散仿真的设置。 波形分析:预配置的范围,用于查看逆变器输出电压、初级/次级电流和整流直流电压。 5.安装与使用 确保您已安装MATLAB和Simulink。 所需工具箱:必须安装Simscape Electrical(以前称为SimPowerSystems)工具箱才能运行sps_lib块。 打开文件并运行模拟。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值