leetcode — distinct-subsequences
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的更多相关文章
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode: Distinct Subsequences [115]
[称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- [LeetCode] Distinct Subsequences [29]
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
随机推荐
- 勾勾街:一个专业的苹果ios app 自助打包的网站,免越狱,免证书签名
众所周知,苹果的APP开发是需要基于MAC环境的,而我们很多的开发者并没有这样的条件,如果单单为发布一款app就去买一台价格昂贵的MAC那成本就太高了! 就算你有一台MAC,也有能力自己开发出一款基于 ...
- hcna(华为)_Telnet篇
Telnet提供了一个交互式操作界面,允许终端远程登录到任何可以充当 Telnet服务器的设备.Telnet用户可以像通过Console口本地登录一样对 设备进行操作.远端Telnet服务器和终端之间 ...
- Do-Now—团队 冲刺博客六
Do-Now-团队 冲刺博客六 作者:仇夏 前言 终于从四级的考试中解脱了(不过我觉得可能凉凉,呵呵),我们的APP制作也迎来了最后的两天. 自己觉得自己其实没有干成什么事情,代码什么的大都是队友们写 ...
- vs中 VMDebugger未能加载导致异常
,纠结了许久的一个问题,终于找到了解决 vs中 VMDebugger未能加载导致异常 错误号:80004005 搜了好多,没有一个给出完美的答案. 解决办法:工具->导入和导出设置,重置一下 ...
- python多线程和多进程使用
# 多线程 from concurrent.futures import ThreadPoolExecutor # 多进程 from concurrent.futures import Process ...
- MUI消息推送
一.push通过H5+实现 简单实现方式:通过轮询服务器是否有新消息推送过来 mui.plusReady(function() { plus.navigator.closeSplashscreen() ...
- linux学习:网络(防火墙)及系统安全相关命令学习
指令: top.htop.free.pstree.lsof.ifconfig.w3m.tcpdump.netstat.nmap.ufw 网络: top #查看内存,cpu,进程之间的状态.hto ...
- Object-C 编程问题汇总
Object-C 编程问题汇总 Cocopods 安装时遇见的问题: error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, ...
- ubuntu Pycharm 2017 3.3 Active
1.打开激活窗口 2.选择 Activate new license with License server (用license server 激活) 3.在 License sever addres ...
- HTML5调用手机摄像机、相册功能 <input>方法
最近用MUI框架做webapp项目,在有PLUS环境的基础上能直接调用手机底层的API来使用拍照或从相册选择上传功能! 在查资料的时候,想起了另一种用input调用摄像和相册功能的方法,之前没有深入了 ...