Skip to content

Commit 897d950

Browse files
author
Partho Biswas
committed
1055 solution updated
1 parent dc43fdf commit 897d950

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ I have solved quite a number of problems from several topics. See the below tabl
283283
|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 | 📌 |
284284
|07| **[55. Jump Game](https://leetcode.com/problems/jump-game/)** | [Python](leetcode.com/python/55_Jump_Game.py)| **[Official](https://leetcode.com/articles/jump-game/)** , [Art 1](https://tinyurl.com/yy9vyjyn)| Medium | 📌 **Must Check. Learned a lot** |
285285
|08| [45. Jump Game II](https://leetcode.com/problems/jump-game-ii/)| [Python](leetcode.com/python/45_Jump_Game_II.py)| [Vid 1](https://www.youtube.com/watch?v=cETfFsSTGJI), [Vid 2](https://www.youtube.com/watch?v=jH_5ypQggWg), [Vid 3](https://www.youtube.com/watch?v=vBdo7wtwlXs), [Art 1](https://tinyurl.com/yalsno6r) | Hard | 📌 |
286-
|09| **[1055. Shortest Way to Form String](https://leetcode.com/problems/shortest-way-to-form-string/)**| [Python](leetcode.com/python/1055_Shortest_Way_to_Form_String.py)| [Art 1](https://tinyurl.com/t6xap4c), [Art 2](https://leetcode.com/problems/shortest-way-to-form-string/discuss/330938/Accept-is-not-enough-to-get-a-hire.-Interviewee-4-follow-up) | Medium | 📌 **Very informative** |
286+
|09| **[1055. Shortest Way to Form String](https://leetcode.com/problems/shortest-way-to-form-string/)**| [Python](leetcode.com/python/1055_Shortest_Way_to_Form_String.py)| [Art 1](https://tinyurl.com/t6xap4c), [Art 2](https://leetcode.com/problems/shortest-way-to-form-string/discuss/330938/Accept-is-not-enough-to-get-a-hire.-Interviewee-4-follow-up) | Medium | 📌 **[Check BS approach again](https://tinyurl.com/t6xap4c)** |
287287

288288

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

leetcode.com/python/1055_Shortest_Way_to_Form_String.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,18 @@ def shortestWay(self, source, target):
6060
sourceCharIndices = defaultdict(list)
6161
for idx, char in enumerate(source):
6262
sourceCharIndices[char].append(idx)
63-
sourceIdx, subsequencesCount = 0, 0
63+
sourceIdx, subsequencesCount = -1, 0
6464
for char in target:
6565
if char not in sourceCharIndices:
6666
return -1
67-
j = bisect.bisect_left(sourceCharIndices[char], sourceIdx) # index in sourceCharIndices[char] that is >= sourceIdx
68-
if j ==len(sourceCharIndices[char]): # wrap around to beginning of source
67+
offsetListForChar = sourceCharIndices[char]
68+
j = bisect.bisect_left(offsetListForChar, sourceIdx) # index in sourceCharIndices[char] that is >= sourceIdx
69+
if j == len(offsetListForChar):
6970
subsequencesCount += 1
70-
j = 0
71-
sourceIdx = sourceCharIndices[char][j] + 1 # next index in source
72-
return subsequencesCount if sourceIdx == 0 else subsequencesCount + 1 # add 1 for partial source
71+
sourceIdx = offsetListForChar[0] + 1
72+
else:
73+
sourceIdx = offsetListForChar[j] + 1
74+
return subsequencesCount
7375

7476

7577

@@ -79,7 +81,7 @@ def shortestWay(self, source, target):
7981

8082

8183
sol = Solution()
82-
source = "abc"
83-
target = "abcbc"
84+
source = "abcab"
85+
target = "aabbaac"
8486
out = sol.shortestWay(source, target)
8587
print('Res: ', out)

0 commit comments

Comments
 (0)