import java.util.Arrays;

/**
*
* Source : https://oj.leetcode.com/problems/distinct-subsequences/
*
*
* 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.
*/
public class DistinctSubsequences { /**
*
* 求解的个数,使用动态规划
*
* 状态:
* i,j表示T中长度为i的prefix:T[0:i-1],S中长度为j的prefix:S[0-j-1],S[j]第j个字符,T[i]第i个字符
* DP[i][j]表示:S[0:j]中包含T[0:i]唯一子串的个数,当j<i的时候DP[i][j] = 0
*
* 递推公式:
* 当S[j] != T[i]的时候
* DP[i+1][j+1] = DP[i+1][j],含义是当前字符不相等的时候,S[j+1]包含T[i+1]的个数就是S[j]包含T[i+1]的个数
*
* 当S[j] == T[i]的时候
* DP[i+1][j+1] = DP[i+1][j] + DP[i][j],含义是当前字符相等的时候,S[j+1]包含T[i+1]的个数就是S[j]包含T[i+1]的个数加上S[j]包含T[i]的个数
*
* 计算方向和起始状态:
* DP[i][j]
* DP[i+1][j],DP[i+1[j+1]
* 从上到下,从左到右
*
* 第0行:1
* 第0列:0
*
*
* @param S
* @param T
* @return
*/
public int distinctSequences (String S, String T) {
int[][] dp = new int[T.length()+1][S.length()+1];
for (int i = 0; i <= T.length(); i++) {
dp[i][0] = 0;
} for (int i = 0; i <= S.length(); i++) {
dp[0][i] = 1;
}
for (int i = 1; i <= T.length(); i++) {
for (int j = i; j <= S.length(); j++) {
if (S.charAt(j-1) == T.charAt(i-1)) {
dp[i][j] = dp[i][j-1] + dp[i-1][j-1];
} else {
dp[i][j] = dp[i][j-1];
}
}
}
return dp[T.length()][S.length()];
} /**
* 优化DP占用空间,因为递推的时候只需要dp[i][j-1],dp[i-1][j-1]
* 也就是当前矩阵左上角的值和左面的值,使用滚动数组优化空间
*
* @param S
* @param T
* @return
*/
public int distinctSequences1 (String S, String T) {
int[] dp = new int[S.length()+1];
Arrays.fill(dp, 1);
for (int i = 1; i <= T.length(); i++) {
int upLeft = dp[0];
dp[0] = 0;
for (int j = 1; j <= S.length(); j++) {
// 相当于记下dp[i-1][j-1]
int temp = dp[j];
// 相当于dp[i][j-1]
dp[j] = dp[j-1];
if (S.charAt(j-1) == T.charAt(i-1)) {
dp[j] += upLeft;
}
upLeft = temp;
}
}
return dp[S.length()];
} public static void main(String[] args) {
DistinctSubsequences subsequences = new DistinctSubsequences();
System.out.println(subsequences.distinctSequences("rabbbit", "rabbit") + "------3");
System.out.println(subsequences.distinctSequences1("rabbbit", "rabbit") + "------3");
}
}

leetcode — distinct-subsequences的更多相关文章

  1. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  2. [LeetCode] Distinct Subsequences 不同的子序列

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

  3. [leetcode]Distinct Subsequences @ Python

    原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...

  4. Leetcode Distinct Subsequences

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

  5. [LeetCode] Distinct Subsequences 解题思路

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

  6. LeetCode: Distinct Subsequences [115]

    [称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...

  7. LeetCode: Distinct Subsequences 解题报告

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

  8. [LeetCode] Distinct Subsequences [29]

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  9. [Leetcode] distinct subsequences 不同子序列

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

  10. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

随机推荐

  1. 解决Idea GitLab Clone failed: Authentication failed for的问题

    刚使用GitLab做项目管理,在idea-check versionControl中使用git clone工程,一直报Clone failed: Authentication failed for ‘ ...

  2. 关于 Mybatis 设置懒加载无效的问题

    看了 mybatis 的教程,讲到关于mybatis 的懒加载的设置: 只需要在 mybatis 的配置文件中设置两个属性就可以了: <settings> <!-- 打开延迟加载的开 ...

  3. Hive与Hbase整合

    Hive与Hbase整合 1.文档 Hive HBase Integration 2.拷贝jar文件 2.1.把Hbase的lib目录下面的jar文件全部拷贝到Hive的lib目录下面 cd /hom ...

  4. sql查询优化策略

    Sql语句执行顺序: 查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_condition (4) WH ...

  5. 2018面向对象程序设计(java)课程学习进度条

    周次 (阅读/编写)代码行数 发布博文量/评论他人博文数量 课余学习时间 学习收获的最大程序阅读或编程任务 1 30-50 1/0 5 九九乘法表 2 60-80 1/0 6 实验一,实验二 3 12 ...

  6. 树莓派3B+上运行.Net Core项目

    最近买了个树莓派3B+,准备把自己写的一个.Net Core爬虫挂在上面跑 买之前没有想到树莓派因为是今年新出的,导致驱动以及系统啥的都不是很完善,导致走了很多弯路,早知道买树莓派3就不用那么多折腾了 ...

  7. Android Studio 直播弹幕

    我只是搬运:https://blog.csdn.net/HighForehead/article/details/55520199 写的很好很详细,挺有参考价值的 demo直通车:https://do ...

  8. linux操作命令之搜索命令

    1.文件搜索命令:locate 文件名 在后台数据库中按照文件名搜索,搜素速度更快 /var/lib/mlocate:#locate命令所搜索的后台数据库 updatedb:更新数据库 updated ...

  9. VBS列出windows更新列表

    Set objSession = CreateObject("Microsoft.Update.Session") Set objSearcher = objSession.Cre ...

  10. json转义 使用 JavaScriptSerializer 时 需要添加的引用

    当创建JavaScriptSerializer创建对象时,JavaScriptSerializer jss=new JavaScriptSerializer():时. 1.   需要添加的是Syste ...