|
| 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