Word Break

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write wordBreak(text, words): return true if text can be split into a sequence of words from the array words, and false if not. A word may be used any number of times. text and the words are lowercase letters a to z, and an empty text gives true. For example, wordBreak("applepenapple", ["apple", "pen"]) is true, and wordBreak("catsandog", ["cats", "dog", "sand", "and", "cat"]) is false. The checks include a text of 500 a's followed by a b, with the words "a" up to "aaaaaaaaaa", where trying every split takes forever: remember which positions in text can be reached.

Your code must pass

  • Splits text into words from the list
  • Tries another split when the first one fails
  • Returns false when no split works
  • Returns true for an empty text
  • Is fast enough for 501 letters