Skip to content

Commit dc43fdf

Browse files
author
Partho Biswas
committed
1055. Shortest Way to Form String
1 parent 0557bb6 commit dc43fdf

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ I have solved quite a number of problems from several topics. See the below tabl
223223
| # | Title | Solution | Tutorial | Level | Remarks |
224224
| --- | --- | --- | --- | --- | --- |
225225
|01| [253_Meeting_Rooms_II](https://leetcode.com/problems/meeting-rooms-ii/)| [Python](leetcode.com/python/253_Meeting_Rooms_II.py)| [Official](https://leetcode.com/problems/meeting-rooms-ii/solution/) | Medium | --- |
226-
|02| [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/solution/)| [Python](leetcode.com/python/347_Top_K_Frequent_Elements.py)| [Official](https://leetcode.com/problems/meeting-rooms-ii/solution/) | Medium | --- |
226+
|02| [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/solution/)| [Python](leetcode.com/python/347_Top_K_Frequent_Elements.py)| [Helper 1](https://stackoverflow.com/questions/19979518/what-is-pythons-heapq-module), [Helper 2](https://stackoverflow.com/a/12373856/2089253), [heapq](https://docs.python.org/3.0/library/heapq.html), [Counter](https://docs.python.org/3/library/collections.html#collections.Counter) | Medium | --- |
227227

228228

229229
### [Tries](https://leetcode.com/explore/learn/card/trie/)
@@ -283,6 +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** |
286287

287288

288289
### [Dynamic Programming](https://www.educative.io/courses/grokking-dynamic-programming-patterns-for-coding-interviews)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from collections import defaultdict
2+
import bisect
3+
4+
class Solution(object):
5+
6+
# # Greedy solution using two pointer. time O(M*N)
7+
# def shortestWay(self, source, target):
8+
# """
9+
# :type source: str
10+
# :type target: str
11+
# :rtype: int
12+
# """
13+
# targetIdx, subsequencesCount = 0, 0
14+
# while targetIdx < len(target):
15+
# sourceIdx = 0
16+
# subsequencesExists = False
17+
# while sourceIdx < len(source) and targetIdx < len(target):
18+
# if source[sourceIdx] == target[targetIdx]:
19+
# sourceIdx += 1
20+
# targetIdx += 1
21+
# subsequencesExists = True
22+
# else:
23+
# sourceIdx += 1
24+
# subsequencesCount += 1
25+
# if not subsequencesExists:
26+
# return -1
27+
# return subsequencesCount
28+
#
29+
#
30+
#
31+
#
32+
# # DP approach using two pointer. time O(M*N)
33+
# def shortestWay(self, source, target):
34+
# """
35+
# :type source: str
36+
# :type target: str
37+
# :rtype: int
38+
# """
39+
# cache = [float('inf')] * (len(target) + 1)
40+
# cache[0] = 0
41+
# for cacheIdx in range(1, len(target) + 1):
42+
# sourceIdx, targetIdx = len(source) - 1, cacheIdx - 1
43+
# while sourceIdx >= 0 and targetIdx >= 0:
44+
# if source[sourceIdx] == target[targetIdx]:
45+
# if cache[targetIdx] != float('inf'):
46+
# cache[cacheIdx] = min(cache[cacheIdx], cache[targetIdx] + 1)
47+
# targetIdx -= 1
48+
# sourceIdx -= 1
49+
# return -1 if cache[-1] == float('inf') else cache[-1]
50+
51+
52+
53+
# Binary Search approach. time O(M*logN) - https://tinyurl.com/t6xap4c
54+
def shortestWay(self, source, target):
55+
"""
56+
:type source: str
57+
:type target: str
58+
:rtype: int
59+
"""
60+
sourceCharIndices = defaultdict(list)
61+
for idx, char in enumerate(source):
62+
sourceCharIndices[char].append(idx)
63+
sourceIdx, subsequencesCount = 0, 0
64+
for char in target:
65+
if char not in sourceCharIndices:
66+
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
69+
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
73+
74+
75+
76+
77+
78+
79+
80+
81+
sol = Solution()
82+
source = "abc"
83+
target = "abcbc"
84+
out = sol.shortestWay(source, target)
85+
print('Res: ', out)

0 commit comments

Comments
 (0)