当前位置:网站首页>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
边栏推荐
猜你喜欢

Use and principle of handlerthread
【架构师(第三十八篇)】 服务端开发之本地安装最新版 MySQL 数据库

sql入门
![[MCU club] design of blind water cup based on MCU [simulation design]](/img/00/eaf57fc003f5b905395c19dee02742.jpg)
[MCU club] design of blind water cup based on MCU [simulation design]
[Architect (Part 38)] locally install the latest version of MySQL database developed by the server

Comics | goodbye, postman! One stop collaboration makes apipost more fragrant!
UI高度自适应的修改方案

Seven mistakes in IT Governance and how to avoid them

Leetcode daily question: implementing strstr()

浏览器缓存库设计总结(localStorage/indexedDB)
随机推荐
BMFONT制作位图字体并在CocosCreator中使用
Nodejs installation and download
Misunderstanding of innovation by enterprise and it leaders
Reprint: VTK notes - clipping and segmentation - irregular closed loop clipping -vtkselectpolydata class (black mountain old demon)
FSS object storage how to access the Intranet
企业和IT领导者对创新的误解
Roson's QT journey 80 qurl class
Cross domain problem of canvas drawing caused by background image cache
用户登录(记住用户)&用户注册(验证码) [运用Cookie Session技术]
Redis常用命令手册
[eight part essay] MySQL
[200 opencv routines] 101 adaptive median filter
最大路径和问题(摘樱桃问题)
Two fresh students: one is practical and likes to work overtime, and the other is skilled. How to choose??
畢業三年的25歲碼農總結
个人买同业存单基金选择什么证券公司开户好,更安全
Reference materials in the process of using Excel
Leetcode daily question: implementing strstr()
MySQL 8.0 above reporting 2058 solution
Is it safe to open an account on great wisdom