From 63397e07cd1afe2ce1f12f0460977c40a1e8c70d Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Sat, 19 Feb 2022 19:58:35 +0530 Subject: [PATCH] feat(392-is-subsequence): simplify and add golang soln --- 0392_is-subsequence/golang/main.go | 21 +++++++++++++++++++ 0392_is-subsequence/python3/main.py | 31 +++++------------------------ 2 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 0392_is-subsequence/golang/main.go diff --git a/0392_is-subsequence/golang/main.go b/0392_is-subsequence/golang/main.go new file mode 100644 index 0000000..5cebad3 --- /dev/null +++ b/0392_is-subsequence/golang/main.go @@ -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 +} diff --git a/0392_is-subsequence/python3/main.py b/0392_is-subsequence/python3/main.py index ca88476..4182fdf 100644 --- a/0392_is-subsequence/python3/main.py +++ b/0392_is-subsequence/python3/main.py @@ -1,9 +1,6 @@ class Solution: def isSubsequence(self, s: str, t: str) -> bool: - slen = len(s) - tlen = len(t) - si = 0 - ti = 0 + slen, tlen, si, ti = len(s), len(t), 0, 0 # If s is longer than t, it's obviously not # going to be a subsequence @@ -11,27 +8,9 @@ class Solution: return False 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 - 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 - - \ No newline at end of file + return si == slen