feat(392-is-subsequence): simplify and add golang soln

This commit is contained in:
Sangeeth Sudheer 2022-02-19 19:58:35 +05:30
parent 106aac6492
commit 63397e07cd
2 changed files with 26 additions and 26 deletions

View File

@ -0,0 +1,21 @@
func isSubsequence(s string, t string) bool {
slen, tlen, si, ti := len(s), len(t), 0, 0
if slen == 0 {
return true
}
if slen > tlen {
return false
}
for ti < tlen && si < slen {
if s[si] == t[ti] {
si += 1
}
ti += 1
}
return si == slen
}

View File

@ -1,9 +1,6 @@
class Solution: class Solution:
def isSubsequence(self, s: str, t: str) -> bool: def isSubsequence(self, s: str, t: str) -> bool:
slen = len(s) slen, tlen, si, ti = len(s), len(t), 0, 0
tlen = len(t)
si = 0
ti = 0
# If s is longer than t, it's obviously not # If s is longer than t, it's obviously not
# going to be a subsequence # going to be a subsequence
@ -11,27 +8,9 @@ class Solution:
return False return False
while ti < tlen and si < slen: while ti < tlen and si < slen:
found = False
while ti < tlen:
if s[si] == t[ti]: if s[si] == t[ti]:
found = True
ti += 1
break
ti += 1
if not found:
return False
si += 1 si += 1
# We reached end of t but we still have ti += 1
# characters remaining in s and hence
# it's not a subsequence
if si < slen:
return False
return True
return si == slen