Skip to content

Commit ae52fac

Browse files
committed
LeetCode 392. Is Subsequence
1 parent 4d93724 commit ae52fac

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ Proposed solutions to some LeetCode problems. The first column links to the prob
3737
| [226. Invert Binary Tree][lc226] | Easy | [python](leetcode/invert-binary-tree.py) |
3838
| [242. Valid Anagram][lc242] | Easy | [python](leetcode/valid-anagram.py) |
3939
| [304. Range Sum Query 2D - Immutable][lc304] | Medium | [python](leetcode/divide_two_integers.py) |
40-
| [406. Queue Reconstruction by Height][lc304] | Medium | [python](leetcode/queue-reconstruction-by-height.py) |
40+
| [392. Is Subsequence][lc392] | Easy | [python](leetcode/is-subsequence.py) |
41+
| [406. Queue Reconstruction by Height][lc406] | Medium | [python](leetcode/queue-reconstruction-by-height.py) |
4142
| [509. Fibonacci Number][lc509] | Easy | [python](leetcode/fibonacci-number.py) |
4243
| [583. Delete Operation for Two Strings][lc583] | Medium | [python](leetcode/delete-operation-for-two-strings.py) |
4344
| [596. Classes More Than 5 Students][lc596] | Easy | [mysql](leetcode/classes_more_than_5_students.sql) |
@@ -96,6 +97,7 @@ Proposed solutions to some LeetCode problems. The first column links to the prob
9697
[lc226]: https://leetcode.com/problems/invert-binary-tree/
9798
[lc242]: https://leetcode.com/problems/valid-anagram/
9899
[lc304]: https://leetcode.com/problems/range-sum-query-2d-immutable/
100+
[lc392]: https://leetcode.com/problems/is-subsequence/
99101
[lc406]: https://leetcode.com/problems/queue-reconstruction-by-height/
100102
[lc509]: https://leetcode.com/problems/fibonacci-number/
101103
[lc583]: https://leetcode.com/problems/delete-operation-for-two-strings/

leetcode/is-subsequence.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# https://leetcode.com/problems/is-subsequence/
2+
3+
import timeit
4+
5+
6+
# Runtime: 48 ms, faster than 57.69% of Python3 online submissions for Is Subsequence.
7+
# Memory Usage: 13.9 MB, less than 44.81 % of Python3 online submissions for Is Subsequence.
8+
class Solution:
9+
def isSubsequence(self, s: str, t: str) -> bool:
10+
if len(s) > len(t):
11+
return False
12+
if not s:
13+
return True
14+
pointer = 0
15+
for char in t:
16+
if pointer == len(s):
17+
return True
18+
if char == s[pointer]:
19+
pointer += 1
20+
return pointer == len(s)
21+
22+
23+
def test():
24+
executor = [
25+
{'executor': Solution, 'title': 'Solution', },
26+
]
27+
tests = [
28+
["", "", True],
29+
["", "abc", True],
30+
["b", "abc", True],
31+
["d", "abcabcabcabcabcd", True],
32+
["d", "abcabcabcabcabc", False],
33+
["abc", "ahbgdc", True],
34+
["axc", "ahbgdc", False],
35+
]
36+
for e in executor:
37+
start = timeit.default_timer()
38+
for _ in range(int(float('1'))):
39+
for t in tests:
40+
sol = e['executor']()
41+
result = sol.isSubsequence(t[0], t[1])
42+
expected = t[2]
43+
assert result == expected, f'{result} != {expected} for {t[0]}:{t[1]} using {e["title"]} solution'
44+
used = str(round(timeit.default_timer() - start, 5))
45+
result = "{0:20}{1:10}{2:10}".format(e['title'], used, "seconds")
46+
print(f"\033[92m» {result}\033[0m")
47+
48+
49+
test()

0 commit comments

Comments
 (0)