当前位置:网站首页>[code Capriccio - dynamic planning] t392 Judgement subsequence
[code Capriccio - dynamic planning] t392 Judgement subsequence
2022-06-24 03:38:00 【Not blogging】
T392、 Judging subsequences (6/23)
Given string s and t , Judge s Is it t The subsequence .
A subsequence of the string is the original string delete some ( It can also be done without deleting ) A new string of characters formed without changing the relative positions of the remaining characters .( for example ,"ace" yes "abcde" A subsequence of , and "aec" No ).
Advanced :
If there is a lot of input S, Referred to as S1, S2, … , Sk among k >= 10 Billion , You need to check in turn whether they are T The subsequence . under these circumstances , How would you change the code ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/is-subsequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
This problem is the same as solving the longest subsequence , I submitted once according to the code of the longest subsequence , Results the correct .
Later reference code Capriccio ideas , Found a location difference , Is that when
s.charAt(i) != s.charAt(j)
In this case , We can't solve the problem like the original longest subsequence dp[i][j] = max(dp[i-1][j], dp[i][j-1])
It is dp[i][j] = dp[i][j-1]
Because at this time S The string of [0:i-1] No longer T[0:j-1] in , So this is equivalent to t To delete an element ,t If the current element t[j - 1] Delete , that dp[i][j] And the value of that is see s[i - 1] And t[j - 2] The comparison results of , namely :dp[i][j] = dp[i][j - 1];
dp
public boolean isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
int[][] dp = new int[n+1][m+1];
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(s.charAt(i-1) == t.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = dp[i][j-1];
}
}
}
return dp[n][m] == s.length()? true:false;
}
Double finger needling

public boolean isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
int i = 0, j =0;
while(i < n && j < m){
if(s.charAt(i) == t.charAt(j)){
i++;
}
j++;
}
return i == n?true:false;
}
边栏推荐
- How to use elastic scaling in cloud computing? What are the functions?
- Cloud development RMB 1 purchase activity is in progress
- Build a small program + management background in 7 days, and this goose factory HR is blessed!
- [see you] on October 24, we met at Tencent Binhai building
- What technology does cloud computing elasticity scale? What are the advantages of elastic scaling in cloud computing?
- Why use code signing? What certificates are required for code signing?
- golang clean a slice
- 4. go deep into tidb: detailed explanation of the implementation process of the implementation plan
- The quick login of QQ cannot be directly invoked through remote login, and the automatic login of QQ can be invoked using VNC
- Community pycharm installation visual database
猜你喜欢
随机推荐
Rasa 3. X learning series -rasa 3.2.0 new release
What is load balancing? What are the functions of load balancing?
高斯光束及其MATLAB仿真
How much is a fortress machine? Why do you need a fortress machine?
A Tencent interview question
Dry goods how to build a data visualization project from scratch?
LeetCode 129. Find the sum of numbers from root node to leaf node
golang clean a slice
Differences between EDI and VMI
Actual combat | how to use micro build low code to realize tolerance application
Using RDM (Remote Desktop Manager) to import CSV batch remote
618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
How to build glasses website what are the functions of glasses website construction
Ar 3D map technology
Use Charles to capture the package of the applet through the mobile agent
web渗透测试----5、暴力破解漏洞--(2)SNMP密码破解
Big coffee face to face | Dr. Chen Guoguo talks about intelligent voice
Supply chain system platform: two management areas
MySQL stored procedure + function
RI Geng series: write a simple shell script, but it seems to have technical content








