392. 判断子序列

方法一
class Solution {
public:
bool isSubsequence(string s, string t) {
int sLength = s.length(), tLength = t.length();
int sIndex = 0, tIndex = 0;
if(sLength == 0){
return true;
}
while(tIndex < tLength){
if(s[sIndex] == t[tIndex]){
sIndex++;
}
if(sIndex == sLength){
return true;
}
tIndex++;
}
return false;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
class Solution {
public:
bool isSubsequence(string s, string t) {
int tIndex= 0, sIndex= 0;
int tLength = t.length(), sLength= s.length();
while(tIndex < tLength && sIndex < sLength){
if(s[sIndex] == t[tIndex]){
sIndex++;
}
tIndex++;
}
return sIndex == sLength;
}
};
方法二
class Solution {
public:
bool isSubsequence(string s, string t){
int n = s.length(),m = t.length();
vector<vector<int>> dp(m + 1,vector<int> (26,0));
for(int i=0;i<26;i++){
dp[m][i] = m;
}
for(int i = m - 1;i>=0;i--) {
for(int j=0;j<26;j++){
if(t[i] == 'a' + j){
dp[i][j] = i;
}else {
dp[i][j] = dp[i + 1][j];
}
}
}
int add = 0;
for(int i = 0;i<n;i++){
if(dp[add][s[i] - 'a'] == m){
return false;
}
add = dp[add][s[i] - 'a'] + 1;
}
return true;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36