Distinct Subsequences Leetcode
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.
初始化nums[s.length()+1][t.length() +1] 数组,
nums[i][j]表示s的前i个字符串中选取t的前j个字符,有多少种方案,
如果t为空,从一个字符串里选出来空串,只有一种方案,就是什么都不选。
如果s为空,从空串里选字符串,方案数为0,由于数组不赋值默认就是0,所以不需要再次初始化这部分。
如果s的第i个字符和t的第j个字符不相等,那么从s的前i个字符里选t的前j个字符的方案就等于s的前i -1个字符里选t的前j个字符的方案数。
如果s的第i个字符和t的第j个字符相等,那么从s的前i个字符里选t的前j个字符的方案就等于s的前i -1个字符里选t的前j个字符的方案数加s的前i -1个字符里选t的前j-1个字符的方案数。
或者这样理解,
假设现在只看前两个字符串的前i个和前j个,用长度为j的字符串去匹配长度为i的字符串有几种方法,
那就可以选择是否匹配最后一位,如果不匹配就去找dp[i-1][j],如果匹配就是dp[i-1][j-1], 加在一起就是此事的结果。
public class Solution {
public int numDistinct(String s, String t) {
if (s == null || t == null) {
return 0;
}
int[][] nums = new int[s.length() + 1][t.length() + 1];
for (int i = 0; i < s.length() + 1; i++) {
nums[i][0] = 1;
}
for (int i = 1; i < s.length() + 1; i++) {
for (int j = 1; j < t.length() + 1; j++) {
nums[i][j] = nums[i - 1][j];
if(s.charAt(i - 1) == t.charAt(j -1)) {
nums[i][j] += nums[i -1][j - 1];
}
}
}
return nums[s.length()][t.length()];
}
}
Distinct Subsequences Leetcode的更多相关文章
- Distinct Subsequences——Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences leetcode java
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- Java for LeetCode 115 Distinct Subsequences【HARD】
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 OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
- leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)
https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
- [leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/ 题意: Given a string S and a string T, co ...
随机推荐
- lunix的查看Tomcat目录下日志的快速操作
可以使用cd命令,cd命令的功能是切换到指定的目录: 命令格式:cd [目录名] 有几个符号作为目录名有特殊的含义: "/"代表根目录. ".."代表上一级目录 ...
- 获取jsp页面的宽和高
var winWidth; var winHeight; function getResult() { if(window.innerWidth) { winWidth=window.innerWid ...
- wpf 线程
一.线程概述:[引用MSDN] 通常,WPF 应用程序从两个线程开始:一个用于处理呈现,一个用于管理 UI.呈现线程有效地隐藏在后台运行,而 UI 线程则接收输入.处理事件.绘制屏幕以及运行应用程序代 ...
- 最短路径(Floyd)算法
#include <stdio.h>#include <stdlib.h>/* Floyd算法 */#define VNUM 5#define MV 65536int P[VN ...
- Python MySQLdb在Linux下的快速安装
在家里windows环境下搞了一次 见 python MySQLdb在windows环境下的快速安装.问题解决方式 http://blog.csdn.NET/wklken/article/deta ...
- easyui tree获取直接子节点而不获取孙子节点方法
$(node.target.nextElementSibling).children().each(function(index,ele){ if(checked){ $('#rcDimTreeRow ...
- Rime 鼠须管 配色方案
android: name: "安卓/Android" author: "Patricivs ipatrickmac@me.com" text_color: 0 ...
- 创建自己的Activity
创建自己的Activity 1.新建class,继承Activity类 2.重写新建类的onCreate 方法 public class SecondActivity extends Activity ...
- CO-类的本质、description方法
类的本质 1. 类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class *Class; 类名就代表着类对象 ...
- Yii2 高级版新建一个 Api 应用
原文地址:http://www.getyii.com/topic/28 先在项目的根目录下复制一份 backend 为 api: cp backend/ api -r 拷贝 api 环境 cp -a ...