Skip to content

Commit 7426332

Browse files
author
Partho Biswas
committed
leetcode 392
1 parent 3c07146 commit 7426332

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ I have solved quite a number of problems from several topics. See the below tabl
279279
|03| [1007. Minimum Domino Rotations For Equal Row](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/)| [Python](leetcode.com/python/Minimum_Domino_Rotations_For_Equal_Row.py)| [Official](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/solution/), [Article 01](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/discuss/252242/JavaC%2B%2BPython-Different-Ideas)| Medium | 📌 Hard to spot if it is a Greedy |
280280
|04| [406. Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)| [Python](leetcode.com/python/406_Queue_Reconstruction_by_Height.py)| [Article 1](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89345/Easy-concept-with-PythonC%2B%2BJava-Solution), [Article 2](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89359/Explanation-of-the-neat-Sort%2BInsert-solution) | Medium | 📌 Fundamentals |
281281
|05| [621. Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Python](leetcode.com/python/621_Task_Scheduler.py)| [Ex 1](https://leetcode.com/problems/task-scheduler/discuss/190390/Can-anyone-explain-to-me-what-the-problem-is-asking), [Ex 2](https://leetcode.com/problems/task-scheduler/discuss/130786/Python-solution-with-detailed-explanation), [Vid 1](https://www.youtube.com/watch?v=hVhOeaONg1Y) | Medium | 📌 Extremely tricky. Not done. Check later |
282-
|06| [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/)| [Python](leetcode.com/python/392_Is_Subsequence.py)| [Ex 1](https://leetcode.com/problems/is-subsequence/discuss/87264/Easy-to-understand-binary-search-solution) | Easy | 📌 |
282+
|06| [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/)| [Python](leetcode.com/python/392_Is_Subsequence.py)| [Ex 1](https://leetcode.com/problems/is-subsequence/discuss/87264/Easy-to-understand-binary-search-solution), [Ex 3](https://leetcode.com/problems/is-subsequence/discuss/87302/Binary-search-solution-for-follow-up-with-detailed-comments/144323) | Easy | 📌 |
283283

284284

285285
### [Dynamic Programming](https://www.educative.io/courses/grokking-dynamic-programming-patterns-for-coding-interviews)

leetcode.com/python/392_Is_Subsequence.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# Using Binary search
55
class Solution(object):
6+
7+
# Code 01
68
def createMap(self, s):
79
# create a map. key is char. value is index of apperance in acending order.
810
posMap = defaultdict(list)
@@ -22,14 +24,29 @@ def isSubsequence(self, s, t):
2224
for char in s:
2325
if char not in posMap: return False
2426
charIndexList = posMap[char]
25-
# try to find an index that is larger than or equal to lowBound
26-
i = bisect_left(charIndexList, lowBound)
27+
# try to find an index that is larger than or equal to lowBound. Means, it returns the index of the first occurrence of lowBound in charIndexList
28+
i = bisect_left(charIndexList, lowBound) # https://www.geeksforgeeks.org/binary-search-bisect-in-python/
2729
if i == len(charIndexList): return False
2830
lowBound = charIndexList[i] + 1
2931
return True
3032

33+
# Code 02
34+
# def isSubsequence(self, s, t):
35+
# idx = defaultdict(list)
36+
# for i, c in enumerate(t):
37+
# idx[c].append(i)
38+
# prev = 0
39+
# for i, c in enumerate(s):
40+
# j = bi.bisect_left(idx[c], prev)
41+
# if j == len(idx[c]): return False
42+
# prev = idx[c][j] + 1
43+
# return True
44+
45+
46+
47+
3148

32-
# Using Two Pointer
49+
# Using Two Pointer
3350
# class Solution(object):
3451
# def isSubsequence(self, s, t):
3552
# """
@@ -53,6 +70,6 @@ def isSubsequence(self, s, t):
5370

5471
sol = Solution()
5572
s = "abc"
56-
t = "ahbgdca"
73+
t = "habgdca"
5774
out = sol.isSubsequence(s, t)
5875
print('Res: ', out)

0 commit comments

Comments
 (0)