当前位置:网站首页>Different subsequence problems I
Different subsequence problems I
2022-06-29 00:52:00 【GreyZeng】
Different subsequence problems I
author :Grey
Original address : Different subsequence problems I
Topic link
LeetCode 115. Different subsequences
Violence solution
Define recursive functions
int process(char[] str, char[] t, int i, int j)
Recursive functions represent :str from i All the way to the end , How many can the generated sequence match t from j The string generated later
therefore process(str,t,0,0) The result is the answer .
Next, consider the... Of recursive functions base case,
if (j == t.length) {
// Express str Have the t The whole thing is done , return 1, It shows that there is a situation
return 1;
}
// Here we are. , explain t It's not over yet
if (i == str.length) {
// str There are no strings anymore ,t It's not the end , therefore , Can't match
return 0;
}
Next up is the universal location , consider str[i] Whether to participate in matching determines the next operation , notes :str[i] If you want to participate in matching , Then we must satisfy str[i] == t[j].
// str[i] Position does not participate in matching
int ans = process(str, t, i + 1, j);
if (str[i] == t[j]) {
// str[i] Participate in , Must satisfy str[i] == t[j]
ans += process(str, t, i + 1, j + 1);
}
The complete code is as follows
public static int numDistinct(String s, String t) {
if (s.length() < t.length()) {
return 0;
}
return process(s.toCharArray(), t.toCharArray(), 0, 0);
}
// str[0.... ending ] Get it done t[0.... ending ]
public static int process(char[] str, char[] t, int i, int j) {
if (j == t.length) {
// It's all done
return 1;
}
if (i == str.length) {
// period , Uncertain
return 0;
}
// no need i The position should be fixed
int ans = process(str, t, i + 1, j);
if (str[i] == t[j]) {
ans += process(str, t, i + 1, j + 1);
}
return ans;
}
This violent solution is LeetCode Upper direct timeout .
Dynamic programming
Two dimensional array
According to the method of violence , You can get , A recursive function has only two mutable arguments , So define two dimensions dp,dp The meaning of is consistent with that of recursive function . therefore dp[0][0] That's the answer. .
int m = str.length;
int n = target.length;
int[][] dp = new int[m + 1][n + 1];
According to the method of violence
if (j == t.length) {
// It's all done
return 1;
}
if (i == str.length) {
// period , Uncertain
return 0;
}
You can get dp The last line of is 1, namely
for (int i = 0; i < m + 1; i++) {
dp[i][n] = 1;
}
Next consider the general dp[i][j], According to the method of violence
int ans = process(str, t, i + 1, j);
if (str[i] == t[j]) {
ans += process(str, t, i + 1, j + 1);
}
You can get ,dp[i][j] rely on dp[i+1][j] and dp[i+1][j+1]( Need to meet str[i] == t[j]) The value of the location .
therefore
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
dp[i][j] = dp[i + 1][j] + (str[i] == target[j] ? dp[i + 1][j + 1] : 0);
}
}
Complete code
public static int numDistinct(String s, String t) {
if (s.length() < t.length()) {
return 0;
}
char[] str = s.toCharArray();
char[] target = t.toCharArray();
int m = str.length;
int n = target.length;
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i < m + 1; i++) {
dp[i][n] = 1;
}
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
dp[i][j] = dp[i + 1][j] + (str[i] == target[j] ? dp[i + 1][j + 1] : 0);
}
}
return dp[0][0];
}
Time complexity O(m*n), among m and n Namely s and t The length of .
Spatial complexity O(m*n), among m and n Namely s and t The length of .
One dimensional array
By analyzing the solution of the above dynamic programming , We can draw a conclusion , A two-dimensional dp The order of calculation is From the last line to the first line , And the current row only depends on the information of a limited number of positions in the previous row , therefore , We can simplify the above two-dimensional table into one-dimensional table , Definition
int m = str.length;
int[] dp = new int[n + 1];
Through the rolling update from the last row to the first row of the one-dimensional table , To get the value of the first row , The complete code is as follows
public static int numDistinct(String s, String t) {
if (s.length() < t.length()) {
return 0;
}
char[] str = s.toCharArray();
char[] target = t.toCharArray();
int m = str.length;
int n = target.length;
int[] dp = new int[n + 1];
dp[n] = 1;
for (int i = m - 1; i >= 0; i--) {
// Pay attention here , From left to right
for (int j = 0; j <= n - 1; j++) {
dp[j] += (str[i] == target[j] ? dp[j + 1] : 0);
}
}
return dp[0];
}
Time complexity O(m*n), among m and n Namely s and t The length of .
Spatial complexity O(n), among n yes t The length of .
more
边栏推荐
- How the slip ring motor works
- 卷绕工艺与叠片工艺的对比
- Is it safe and reliable for qiniu school to help open a securities account? How to drive
- Is l1-031 too fat (10 points)
- [200 opencv routines] 101 adaptive median filter
- 请问基金是否靠谱,安全吗
- BMFONT制作位图字体并在CocosCreator中使用
- 2022_ 2_ 16 the second day of learning C language_ Constant, variable
- Leetcode daily question: implementing strstr()
- [staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)
猜你喜欢

深度优先搜索实现抓牛问题
![[staff] accent mark, gradually stronger mark and gradually weaker mark](/img/5d/5738bd5503d7ed0621932f901c2e8d.jpg)
[staff] accent mark, gradually stronger mark and gradually weaker mark

MySQL 8.0 above reporting 2058 solution

Getting started with SQL

FSS object storage how to access the Intranet

Nodejs安装和下载

Check the open source projects of yyds in June!

Daily practice: delete duplicates in the ordered array

Seven mistakes in IT Governance and how to avoid them
![[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)](/img/5d/bee9a6e50384a7e05e9a71e9fbed9a.jpg)
[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)
随机推荐
Go1.18 new feature: discard strings Title Method, a new pit!
大型网站架构基础之笔记
戴口罩人脸数据集和戴口罩人脸生成方法
畢業三年的25歲碼農總結
Two fresh students: one is practical and likes to work overtime, and the other is skilled. How to choose??
Operation level smart campus system source code smart campus applet source code + electronic class card + face recognition system
What is contemporaneous group analysis? Teach you to use SQL to handle
be based on. NETCORE development blog project starblog - (13) add friendship link function
Daily question 1: remove elements
最新Justnews主题源码6.0.1开心版+社交问答插件2.3.1+附教程
Document management.
盘点 6 月 yyds 的开源项目!
Is it safe and reliable for qiniu school to help open a securities account? How to drive
用户登录(记住用户)&用户注册(验证码) [运用Cookie Session技术]
Mapbox GL loading local publishing DEM data
【leetcode】153. Find the lowest value in the rotation sort array
Xuetong denies that the theft of QQ number is related to it: it has been reported; IPhone 14 is ready for mass production: four models are launched simultaneously; Simple and elegant software has long
[MCU club] design of GSM version of range hood based on MCU [simulation design]
卷绕工艺与叠片工艺的对比
sql入门