动态规划
class Solution { public boolean isSubsequence(String s, String t) { boolean table[][] = new boolean[s.length() + 1][t.length() + 1]; for (int col=0; col<table[0].length; col++) { table[0][col] = true; } for (int row=1; row<table.length; row++) { char ch1 = s.charAt(row-1); for (int col=1; col<table[row].length; col++) { char ch2 = t.charAt(col-1); if (ch1==ch2) { table[row][col] = table[row-1][col-1]; } else { table[row][col] = table[row][col-1]; } } } boolean[] lastRow = table[table.length-1]; return lastRow[lastRow.length-1]; }}
参考链接: https://leetcode-cn.com/problems/is-subsequence/solution/shi-pin-jiang-jie-dong-tai-gui-hua-qiu-jie-is-subs/
双指针
class Solution { public boolean isSubsequence(String s, String t) { int m = s.length(); int n = t.length(); int i = 0,j = 0; while(i < m && j < n){ if(s.charAt(i) == t.charAt(j)){ i++; } j++; } return i == m; }}作者:jovial-hugleufk链接:https://leetcode-cn.com/problems/is-subsequence/solution/shuang-zhi-zhen-by-jovial-hugleufk-mogi/来源:力扣(LeetCode)著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。