392.判断子序列
题目链接:392. 判断子序列 - 力扣(LeetCode)
class Solution {
public boolean isSubsequence(String t, String s) {
int j = 0;
if(t.length() > s.length()) {
return false;
}
if(t.length() == 0) {
return true;
}
for(int i = 0; i < s.length(); ++i) {
if(t.charAt(j) == s.charAt(i)) {
++j;
}
if(s.length() - i < t.length() - j) {
return false;
}
if(j == t.length()) {
return true;
}
}
return false;
}
}
115.不同的子序列
题目链接: 115. 不同的子序列 - 力扣(LeetCode)
class Solution {
public int numDistinct(String s, String t) {
int slen = s.length();
int tlen = t.length();
int[][] dp = new int[slen + 1][tlen + 1];
for(int i = 0; i <= slen; ++i) {
dp[i][0] = 1;
}
for(int i = 1; i <= slen; ++i) {
for(int j = 1; j <= tlen; ++j){
if(s.charAt(i-1) == t.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
}else {
dp[i][j] = dp[i-1][j];
}
}
}
return dp[slen][tlen];
}
}