classSolution { public: boolisSubsequence(string s, string t){ int n = s.length(), m = t.length(); int i = 0, j = 0; while (i < n && j < m) { if (s[i] == t[j]) { i++; } j++; } return i == n; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { publicbooleanisSubsequence(String s, String t) { intn= s.length(), m = t.length(); inti=0, j = 0; while (i < n && j < m) { if (s.charAt(i) == t.charAt(j)) { i++; } j++; } return i == n; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9
classSolution: defisSubsequence(self, s: str, t: str) -> bool: n, m = len(s), len(t) i = j = 0 while i < n and j < m: if s[i] == t[j]: i += 1 j += 1 return i == n
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11
funcisSubsequence(s string, t string)bool { n, m := len(s), len(t) i, j := 0, 0 for i < n && j < m { if s[i] == t[j] { i++ } j++ } return i == n }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11
boolisSubsequence(char* s, char* t) { int n = strlen(s), m = strlen(t); int i = 0, j = 0; while (i < n && j < m) { if (s[i] == t[j]) { i++; } j++; } return i == n; }
funcisSubsequence(s string, t string)bool { n, m := len(s), len(t) f := make([][26]int, m + 1) for i := 0; i < 26; i++ { f[m][i] = m } for i := m - 1; i >= 0; i-- { for j := 0; j < 26; j++ { if t[i] == byte(j + 'a') { f[i][j] = i } else { f[i][j] = f[i + 1][j] } } } add := 0 for i := 0; i < n; i++ { if f[add][int(s[i] - 'a')] == m { returnfalse } add = f[add][int(s[i] - 'a')] + 1 } returntrue }