feat(392-is-subsequence): simplify and add golang soln
This commit is contained in:
parent
106aac6492
commit
63397e07cd
21
0392_is-subsequence/golang/main.go
Normal file
21
0392_is-subsequence/golang/main.go
Normal 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
|
||||||
|
}
|
@ -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
|
if s[si] == t[ti]:
|
||||||
|
si += 1
|
||||||
while ti < tlen:
|
|
||||||
if s[si] == t[ti]:
|
|
||||||
found = True
|
|
||||||
ti += 1
|
|
||||||
break
|
|
||||||
|
|
||||||
ti += 1
|
|
||||||
|
|
||||||
if not found:
|
|
||||||
return False
|
|
||||||
|
|
||||||
si += 1
|
|
||||||
|
|
||||||
# We reached end of t but we still have
|
|
||||||
# characters remaining in s and hence
|
|
||||||
# it's not a subsequence
|
|
||||||
if si < slen:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
ti += 1
|
||||||
|
|
||||||
|
return si == slen
|
||||||
|
Loading…
Reference in New Issue
Block a user