Back to DSA
Word Break
mediumGiven a string s and a collection of dictionary words, decide whether s can be decomposed into a sequence of one or more words that all appear in the dictionary. Each dictionary word may be used more than once.
Examples
Example 1:
Input:
s = "applepenapple", wordDict = ["apple","pen"]Output:
trueExplanation: "applepenapple" splits into "apple" + "pen" + "apple".
Example 2:
Input:
s = "pineapplepen", wordDict = ["pine","apple","pen","pineapple"]Output:
trueExplanation: Can be split as "pineapple" + "pen" or "pine" + "apple" + "pen".
Hints
1234567