Created
          December 8, 2020 02:26 
        
      - 
      
- 
        Save iJustErikk/22161124ab045b255a11769a5950e0d8 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const allConstruct = (target, wordBank) => { | |
| const table = new Array(target.length + 1) | |
| .fill().map(() => []); | |
| table[0] = [[]]; | |
| for (let i = 0; i <= target.length; i += 1) { | |
| const oldCombinations = table[i]; | |
| if (oldCombinations.length === 0) continue; | |
| const rest = target.slice(i); | |
| for (let word of wordBank) { | |
| if (!rest.startsWith(word)) continue; | |
| const lengthAdded = word.length; | |
| const newCombinations = table[i + lengthAdded]; | |
| for (let currentArr of oldCombinations) { | |
| newCombinations.push([...currentArr, word]); | |
| } | |
| } | |
| } | |
| return table[target.length]; | |
| }; | |
| console.time(); | |
| console.log(allConstruct("dog", ["a", "b", "c", "d", "o", "g"])); | |
| console.log(allConstruct("whatwhatwhatarearewhatamong rubble", ["what", "What ", "are", "are ", "we", "we ", "among ", "rubble"])); | |
| console.log(allConstruct("abcdef", ["ab", "abc", "cd", "def", "abcd"])); | |
| console.log(allConstruct("manhotdog", ["man", "hot", "dog", "manh", "ot", "d", "o", "g"])); | |
| console.log(allConstruct("eeeeeeeeeeeeeeeeep", ["e", "ee", 'eee', 'eeee'])); | |
| console.timeEnd(); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment