当前位置:网站首页>[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;
}
边栏推荐
- What does cloud computing elasticity mean? What are its functions?
- Using RDM (Remote Desktop Manager) to import CSV batch remote
- "Sharp weapon" for enterprise resumption? When the sale comes, the contract should be signed like this!
- The importance of the computer room to the stable operation of the server
- Tencent cloud CIF engineering effectiveness summit was successfully opened, and coding released a series of new products
- Paste board based on curl and COS
- Grpc: how to add API log interceptors / Middleware?
- Grpc: how to make grpc provide swagger UI?
- Create a telepresence USB drive using the DD command
- How does cloud computing achieve elastic scaling? What are the characteristics of elasticity?
猜你喜欢

元气森林推“有矿”,农夫山泉们跟着“卷”?
Thank you for your recognition! One thank-you note after another

ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0

618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?

Modstartcms theme introductory development tutorial

你了解TLS协议吗?

Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)

On Sunday, I rolled up the uni app "uview excellent UI framework"

内存泄漏之KOOM

浅谈游戏安全 (一)
随机推荐
What are the advantages of EIP? What is the relationship between EIP and fixed IP?
左滑从小窗到大窗口DispatchFrameLayout
Actual battle case | refuse information disclosure, Tencent cloud helps e-commerce fight against web crawlers
Using RDM (Remote Desktop Manager) to import CSV batch remote
hprofStringCache
Grpc: how to make grpc provide restful API services?
TRTC audio quality problem
take the crown! Tencent security won the 2021 national network security week outstanding innovation achievement award
Cross platform RDP protocol, RDP like protocol and non RDP protocol remote software
Some basic knowledge of data center server cabinet
EIP maximum EIP EIP remote desktop access
golang clean a slice
Building RPM packages - spec Basics
getLocationInWindow源码
Why install code signing certificate to scan and eliminate virus software from security
[see you] on October 24, we met at Tencent Binhai building
What is the price of the elastic public network IP bandwidth
The importance of the computer room to the stable operation of the server
What does elastic public IP mean? The advantages of elastic public IP
Create a telepresence USB drive using the DD command