|
| 1 | +# allConstruct |
| 2 | +# from dynamic programming video YT |
| 3 | +# https://www.youtube.com/watch?v=oBt53YbR9Kk&t=11470s |
| 4 | +# Timestamp: 05:10:01 |
| 5 | + |
| 6 | + |
| 7 | +# ------------------ i guess this is top down approach ---------------------- |
| 8 | +ansFin = [] |
| 9 | +def allConstruct(target, wordBank, currList = []): |
| 10 | + |
| 11 | + if target == '': |
| 12 | + return True |
| 13 | + |
| 14 | + for currWord in wordBank: |
| 15 | + if(target.startswith(currWord)): |
| 16 | + currList += [currWord] |
| 17 | + remainingTarget = target[len(currWord):] |
| 18 | + res = allConstruct(remainingTarget, wordBank, currList) |
| 19 | + if res == True: |
| 20 | + ansFin.append(currList.copy()) |
| 21 | + currList.pop() |
| 22 | + |
| 23 | + |
| 24 | +allConstruct('abcdef', ['ab', 'cd', 'cde', 'ef', 'def', 'abcd', 'f']) |
| 25 | +# allConstruct('purple', ['purp', 'p', 'ur', 'le', 'purpl']) |
| 26 | +# allConstruct('happ', ['cat', 'p', 'dog', 'le', 'mouse']) # this should give [[]] but gives [] |
| 27 | +print(ansFin) |
| 28 | + |
| 29 | + |
| 30 | +# ------------ approach in the video ---------------------------------------------- |
| 31 | +# this gives the o/p, but return early hence only gives one occurance |
| 32 | +# not all |
| 33 | + |
| 34 | +ansFin = [] |
| 35 | +def allConstruct(target, wordBank): |
| 36 | + if target == '': |
| 37 | + return [] |
| 38 | + currList = [] |
| 39 | + for currWord in wordBank: |
| 40 | + if(target.startswith(currWord)): |
| 41 | + remainingTarget = target[len(currWord):] |
| 42 | + res = allConstruct(remainingTarget, wordBank) |
| 43 | + |
| 44 | + if res is not None: |
| 45 | + currList += [currWord] + res |
| 46 | + return currList |
| 47 | + print(ansFin) |
| 48 | + ansFin.append(currList) |
| 49 | + |
| 50 | +print('ths gives one occurance only coz of early return') |
| 51 | +print(allConstruct('abcdef', ['ab', 'cd', 'cde', 'ef', 'def', 'abcd', 'f'])) |
| 52 | +# print(ansFin) |
| 53 | + |
| 54 | + |
| 55 | +#----------------- from gemini and this works -_- -------------------------------------- |
| 56 | +# without DP |
| 57 | +# check the explaination in the chat: https://g.co/gemini/share/479ee118861d |
| 58 | + |
| 59 | + |
| 60 | +def allConstruct(target, wordBank): |
| 61 | + if target == "": |
| 62 | + return [[]] # Base case: empty target has one combination (the empty list) |
| 63 | + |
| 64 | + all_combinations = [] |
| 65 | + for word in wordBank: |
| 66 | + if target.startswith(word): |
| 67 | + remaining = target[len(word):] |
| 68 | + remaining_combinations = allConstruct(remaining, wordBank) |
| 69 | + for combo in remaining_combinations: |
| 70 | + all_combinations.append([word] + combo) |
| 71 | + print(all_combinations) |
| 72 | + |
| 73 | + return all_combinations |
| 74 | + |
| 75 | +result = allConstruct('abcdef', ['ab', 'cd', 'cde', 'ef', 'def', 'abcd', 'f']) |
| 76 | +print(result) |
| 77 | + |
| 78 | + |
| 79 | +# ----------------- gemini solution with DP -------------------------- |
| 80 | + |
| 81 | +def allConstruct(target, wordBank, memo=None): |
| 82 | + if memo is None: |
| 83 | + memo = {} # Initialize memoization dictionary |
| 84 | + |
| 85 | + if target == "": |
| 86 | + return [[]] # Base case: empty target has one combination (the empty list) |
| 87 | + |
| 88 | + if target in memo: |
| 89 | + return memo[target] # Use memoized result if available |
| 90 | + |
| 91 | + all_combinations = [] |
| 92 | + for word in wordBank: |
| 93 | + if target.startswith(word): |
| 94 | + remaining = target[len(word):] |
| 95 | + remaining_combinations = allConstruct(remaining, wordBank, memo) |
| 96 | + for combo in remaining_combinations: |
| 97 | + all_combinations.append([word] + combo) # Add current word to each combination |
| 98 | + |
| 99 | + memo[target] = all_combinations # Store result for memoization |
| 100 | + return all_combinations |
| 101 | + |
| 102 | +# result = allConstruct('aaaaaa', ['a', 'aaa', 'aaaa', 'aaaaaa']) |
| 103 | +# print(result) |
0 commit comments