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. PRTG测试!

    http://www.paessler.com/prtg. 我的效果图:

  2. jquery中$.each()的用法

    each()函数是基本上所有的框架都提供了的一个工具类函数,通过它,你可以遍历对象.数组的属性值并进行处理.jQuery和jQuery对象都实 现了该方法,对于jQuery对象,只是把each方法简单 ...

  3. 反沙箱——SetErrorMode

    目录 1.前言 2.原理讲解 3.代码实现 4.参考 1.前言 利用SetErrorMode进行反沙箱的技术,在2010年就有被提出,但是之前搜了很久都没有相关内容,这里简单的说一下这个反沙箱的实现. ...

  4. 「深度剖析」程序员因为奇葩需求暴打pm,然后被双双开除

    想必大家都听说了,这两天关于中国平安一个产品经理因奇葩需求和程序员爆发肢体冲突的事件在朋友圈被刷屏,更有现场打架视频在技术群里疯传. 在这里先带大家简单文字回顾下事情经过,N次打架视频和截图就不给大家 ...

  5. 【高并发架构】Redis特点及构件模型

    数据结构 redis 相比 memcached 来说,拥有更多的数据结构,能支持更丰富的数据操作.如果需要缓存能够支持更复杂的结构和操作, redis 会是不错的选择. redis 主要有以下几种数据 ...

  6. Burp Suite Pro 教程

    1.Burp Suite Pro2.0.11破解版-2018.11.06更新 说明基地址 来源:http://ximcx.cn/post-110.html 启动;如果是用的burp2.0,把下面的代码 ...

  7. 问题:AJAX的send参数里,空格以及它后面的数据在传递时消失(已解决)

    今天,我在写关于前端向后台传递一些数字进行排序,并把结果返回到前端的程序.我用的是POST方式,须要在send里面写键值对. 前端和后台都写完,但是,前端传递给后台的数据是一部分,我检查了下,发现获取 ...

  8. getMemory的经典例子

    //NO.1:程序首先申请一个char类型的指针str,并把str指向NULL(即str里存的是NULL的地址,*str为NULL中的值为0),调用函数的过程中做了如下动作:1申请一个char类型的指 ...

  9. Spring源码工程导入Eclsipse缺少两个jar文件

    按照<Spring源码深度解析>所述,使用gradle cleanidea eclipse将Spring源码转为eclipse工程后,导入eclipse,最后发现还是缺少spring-cg ...

  10. [IOT] 自制蓝牙工牌办公室定位系统 (二)—— 基于ESP32的蓝牙信号扫描系统

      前面章节: 自制蓝牙工牌办公室定位系统 (一)-- 阿里物联网平台概览及打通端到云(硬核·干货)   目录: 1.蓝牙广播简介 2.蓝牙扫描简介 3.基于蓝牙广播和蓝牙扫描常见应用 4.ESP32 ...