|
| 1 | +/** |
| 2 | + * 02:47:30 - https://youtu.be/oBt53YbR9Kk?t=10050 |
| 3 | + * |
| 4 | + * |
| 5 | + * Problem: |
| 6 | + * Write a function `allConstruct(target, wordBank)` that accepts a target string and an array of strings. |
| 7 | + * This function should return a 2D array containing all the ways that `target` can be constructed by concatenating elements of the `wordBank` array. |
| 8 | + * Each element of the 2D array should represent one combination that constructs the `target` |
| 9 | + * You may reuse elements of `wordBank` as many times as needed. |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Brute Force Approach |
| 14 | + * This function attempts to solve the problem using recursion without any optimizations. |
| 15 | + * @param {string} target - The target string we want to construct. |
| 16 | + * @param {string[]} wordBank - The array of words that can be used to construct the target. |
| 17 | + * @returns {string[][]} - Returns a 2D array containing all the ways to construct the target. |
| 18 | + */ |
| 19 | +function allConstruct(target, wordBank) { |
| 20 | + // Base case: If the target is an empty string, return an array containing an empty array |
| 21 | + if (target === '') return [[]]; |
| 22 | + |
| 23 | + const result = []; |
| 24 | + |
| 25 | + // Iterate over each word in the wordBank |
| 26 | + for (const word of wordBank) { |
| 27 | + // Check if the word is a prefix of the target |
| 28 | + if (target.indexOf(word) === 0) { |
| 29 | + // If it is, we create a new target by removing the prefix (word) |
| 30 | + const suffix = target.slice(word.length); |
| 31 | + // Recursively find all ways to construct the suffix |
| 32 | + const suffixWays = allConstruct(suffix, wordBank); |
| 33 | + // For each way to construct the suffix, add the current word to the beginning |
| 34 | + const targetWays = suffixWays.map(way => [word, ...way]); |
| 35 | + // Add all the ways to construct the target to the result array |
| 36 | + result.push(...targetWays); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +// Brute Force |
| 44 | +// m = target.length |
| 45 | +// n = wordBank.length |
| 46 | +// Time Complexity: O(n^m) |
| 47 | +// Each recursive call spawns n new recursive calls, leading to exponential time complexity. |
| 48 | +// Space Complexity: O(m) |
| 49 | +// The maximum depth of the recursion tree is m. |
| 50 | +console.log("===== Brute Force ====="); |
| 51 | +console.log(allConstruct("purple", ["purp", "p", "ur", "le", "purpl"])); // [ [ 'purp', 'le' ], [ 'p', 'ur', 'p', 'le' ] ] |
| 52 | +console.log(allConstruct("abcdef", ["ab", "abc", "cd", "def", "abcd", "ef", "c"])); // [ [ 'ab', 'cd', 'ef' ], [ 'ab', 'c', 'def' ], [ 'abc', 'def' ], [ 'abcd', 'ef' ] ] |
| 53 | +console.log(allConstruct('skateboard', ['bo', 'rd', 'ate', 't', 'ska', 'sk', 'boar'])); // [] |
| 54 | +console.log(allConstruct('aaaaaaaaaaaaaaaaaaaaaaaaaaz', ['a', 'aa', 'aaa', 'aaaa', 'aaaaa', 'aaaaaa'])); // [] |
| 55 | + |
| 56 | +/** |
| 57 | + * Optimized Approach using Memoization |
| 58 | + * This function optimizes the brute force approach by using memoization to store the results of subproblems. |
| 59 | + * @param {string} target - The target string we want to construct. |
| 60 | + * @param {string[]} wordBank - The array of words that can be used to construct the target. |
| 61 | + * @param {object} memo - An object used to store the results of subproblems. |
| 62 | + * @returns {string[][]} - Returns a 2D array containing all the ways to construct the target. |
| 63 | + */ |
| 64 | +function optimizedAllConstruct(target, wordBank, memo = {}) { |
| 65 | + // Check if the result for the current target is already in the memo |
| 66 | + if (target in memo) return memo[target]; |
| 67 | + // Base case: If the target is an empty string, return an array containing an empty array |
| 68 | + if (target === '') return [[]]; |
| 69 | + |
| 70 | + const result = []; |
| 71 | + |
| 72 | + // Iterate over each word in the wordBank |
| 73 | + for (const word of wordBank) { |
| 74 | + // Check if the word is a prefix of the target |
| 75 | + if (target.indexOf(word) === 0) { |
| 76 | + // If it is, we create a new target by removing the prefix (word) |
| 77 | + const suffix = target.slice(word.length); |
| 78 | + // Recursively find all ways to construct the suffix |
| 79 | + const suffixWays = optimizedAllConstruct(suffix, wordBank, memo); |
| 80 | + // For each way to construct the suffix, add the current word to the beginning |
| 81 | + const targetWays = suffixWays.map(way => [word, ...way]); |
| 82 | + // Add all the ways to construct the target to the result array |
| 83 | + result.push(...targetWays); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Store the result in the memo and return the total count |
| 88 | + memo[target] = result; |
| 89 | + return result; |
| 90 | +} |
| 91 | + |
| 92 | +console.log("===== Memoization ====="); |
| 93 | +// Memoization |
| 94 | +// m = target.length |
| 95 | +// n = wordBank.length |
| 96 | +// Time Complexity: O(n * m) |
| 97 | +// Each target string can be constructed in a number of ways proportional to its length, leading to polynomial time complexity. |
| 98 | +// Space Complexity: O(m) |
| 99 | +// Due to the memo object and the call stack. |
| 100 | +console.log(optimizedAllConstruct("purple", ["purp", "p", "ur", "le", "purpl"])); // [ [ 'purp', 'le' ], [ 'p', 'ur', 'p', 'le' ] ] |
| 101 | +console.log(optimizedAllConstruct("abcdef", ["ab", "abc", "cd", "def", "abcd", "ef", "c"])); // [ [ 'ab', 'cd', 'ef' ], [ 'ab', 'c', 'def' ], [ 'abc', 'def' ], [ 'abcd', 'ef' ] ] |
| 102 | +console.log(optimizedAllConstruct('skateboard', ['bo', 'rd', 'ate', 't', 'ska', 'sk', 'boar'])); // [] |
| 103 | +console.log(optimizedAllConstruct('aaaaaaaaaaaaaaaaaaaaaaaaaaaz', ['a', 'aa', 'aaa', 'aaaa', 'aaaaa', 'aaaaaa'])); // [] |
0 commit comments