is_subsequence
1# @leet start 2class Solution: 3 def isSubsequence(self, s: str, t: str) -> bool: 4 """ 5 This question asks us if `s` is a subsequence of `t`. To find that out, 6 we can use two pointers. We start out at the beginning of `s` and `t`. 7 If the current char of `s`, $s_i$ is equal to the current char of `t`, 8 $t_j$, we increment both `i` and `j` since we've found a match. Otherwise, 9 this is a miss, so we only increment `j`, since we want to continue on 10 for `t`. Finally, we make sure we've reached the end of `s`, so we make 11 sure that `i` is equal to the length of `s`. 12 """ 13 i, j, m, n = 0, 0, len(s), len(t) 14 15 while i < m and j < n: 16 l, r = s[i], t[j] 17 if l == r: 18 i += 1 19 j += 1 20 return i == m 21 22 23# @leet end 24 25 26def test(): 27 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def isSubsequence(self, s: str, t: str) -> bool: 5 """ 6 This question asks us if `s` is a subsequence of `t`. To find that out, 7 we can use two pointers. We start out at the beginning of `s` and `t`. 8 If the current char of `s`, $s_i$ is equal to the current char of `t`, 9 $t_j$, we increment both `i` and `j` since we've found a match. Otherwise, 10 this is a miss, so we only increment `j`, since we want to continue on 11 for `t`. Finally, we make sure we've reached the end of `s`, so we make 12 sure that `i` is equal to the length of `s`. 13 """ 14 i, j, m, n = 0, 0, len(s), len(t) 15 16 while i < m and j < n: 17 l, r = s[i], t[j] 18 if l == r: 19 i += 1 20 j += 1 21 return i == m
def
isSubsequence(self, s: str, t: str) -> bool:
4 def isSubsequence(self, s: str, t: str) -> bool: 5 """ 6 This question asks us if `s` is a subsequence of `t`. To find that out, 7 we can use two pointers. We start out at the beginning of `s` and `t`. 8 If the current char of `s`, $s_i$ is equal to the current char of `t`, 9 $t_j$, we increment both `i` and `j` since we've found a match. Otherwise, 10 this is a miss, so we only increment `j`, since we want to continue on 11 for `t`. Finally, we make sure we've reached the end of `s`, so we make 12 sure that `i` is equal to the length of `s`. 13 """ 14 i, j, m, n = 0, 0, len(s), len(t) 15 16 while i < m and j < n: 17 l, r = s[i], t[j] 18 if l == r: 19 i += 1 20 j += 1 21 return i == m
This question asks us if s is a subsequence of t. To find that out,
we can use two pointers. We start out at the beginning of s and t.
If the current char of s, $s_i$ is equal to the current char of t,
$t_j$, we increment both i and j since we've found a match. Otherwise,
this is a miss, so we only increment j, since we want to continue on
for t. Finally, we make sure we've reached the end of s, so we make
sure that i is equal to the length of s.
def
test():