[Leetcode] Distinct Subsequences (Java)
来源:互联网 发布:express mysql 编辑:程序博客网 时间:2024/05/02 19:13
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).
Here is an example:
S = "rabbbit"
, T = "rabbit"
Return 3
.
DP,num[i][j] 保存S(0,i)包含的T(0,j)的数量
S.charAt(i)==T.charAt(j) 时,num[i][j]=num[i-1][j-1]+num[i-1][j];
S.charAt(i)!=T.charAt(j) 时,num[i][j]=num[i-1][j]。
public class Solution { public int numDistinct(String S, String T) { if(S.length()==0||T.length()==0)return 0;int[][] num = new int[S.length()][T.length()];num[0][0]=S.charAt(0)==T.charAt(0)?1:0;for(int i=1;i<S.length();i++)num[i][0]=S.charAt(i)==T.charAt(0)?num[i-1][0]+1:num[i-1][0];for(int j=1;j<T.length();j++){for(int i=1;i<S.length();i++){if(S.charAt(i)==T.charAt(j))num[i][j]=num[i-1][j-1]+num[i-1][j];else num[i][j]=num[i-1][j];}}return num[S.length()-1][T.length()-1]; }}
0 0
- [Leetcode] Distinct Subsequences (Java)
- Leetcode Distinct Subsequences Java
- [LeetCode][Java] Distinct Subsequences
- [leetcode-115]Distinct Subsequences(java)
- 【leetcode】115. Distinct Subsequences【java】
- [leetcode]115. Distinct Subsequences(Java)
- LeetCode – Distinct Subsequences Total (Java)
- LeetCode Distinct Subsequences
- [LeetCode]Distinct Subsequences
- [Leetcode] Distinct Subsequences *
- [leetcode] Distinct Subsequences
- [LeetCode] Distinct Subsequences
- [Leetcode] Distinct Subsequences
- leetcode 111: Distinct Subsequences
- LeetCode Distinct Subsequences
- [leetcode] Distinct Subsequences
- LeetCode - Distinct Subsequences
- Leetcode:Distinct Subsequences
- NYOJ 638 Take it easy II
- Effective C++(19) 设计class犹如设计type
- Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务
- 马年工作 First Day
- 祝贺2014年,系统集成项目经理培训报名表
- [Leetcode] Distinct Subsequences (Java)
- 李开复:算法的力量
- Adding Background Fetch Capabilities to Your Apps(在后台获取数据)
- linux【文件与文件夹命令】
- 虚函数 多重继承的构造函数的调用,虚函数地址的找法
- MFC 多线程总结
- 为SFTP/SSH/SCP访问设置Public Key认证
- Qt之2D绘图
- Thrift VS Protocol buffers