Created
          December 7, 2020 21:30 
        
      - 
      
- 
        Save iJustErikk/f119b8181d5e4a874f1f24a9af2f5011 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 canConstruct = (target, wordBank) => { | |
| const table = new Array(target.length + 1).fill(false); | |
| // for each index of table, looking at that index | |
| // will be looking at the substring up to but not including that value | |
| // 0 would be true | |
| table[0] = true; | |
| for (let i = 0; i <= target.length; i += 1) { | |
| if (!table[i]) continue; | |
| const rest = target.slice(i); | |
| for (let word of wordBank) { | |
| if (!rest.startsWith(word)) continue; | |
| const spacesAhead = word.length; | |
| table[i + spacesAhead] = true; | |
| } | |
| } | |
| // O(w * W * len(target)), where W is len of longest word in wordbank | |
| return table[target.length] | |
| }; | |
| console.time(); | |
| console.log(canConstruct("abcdef", ["ab", "abc", "cd", "def", "abcd"])); // -> true(abc, def) | |
| console.log(canConstruct("skateboard", ["sk", "kat", "board", "boar", "s", "z", "d", "l", "k", "4", "54"])); // -> false | |
| console.log(canConstruct("whatwhatwhatarearewhatamong rubble", ["what", "What ", "are", "are ", "we", "we ", "among ", "rubble"])); // -> true | |
| console.log(canConstruct("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeg", ["e", "ee", 'eee', 'eeee'])); | |
| console.timeEnd(); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment