392 Is Subsequence
392. Is Subsequence
1. Question
2. Implementation
class Solution {
public boolean isSubsequence(String s, String t) {
int m = s.length(), n = t.length();
boolean[][] dp = new boolean[s.length() + 1][t.length() + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = true;
}
else if (j == 0) {
dp[i][j] = false;
}
else if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[m][n];
}
}3. Time & Space Complexity
Last updated