27. Word Break
HardAlgorithms~35 min
Given a string and a list of words, decide whether the string can be segmented into a sequence of one or more of those words.
Words may be reused as many times as you like.
Examples
Example 1
- Input:
- str = "leetcode", words = ["leet", "code"]
- Output:
- true
Example 2
- Input:
- str = "applepenapple", words = ["apple", "pen"]
- Output:
- true
- Why:
- "apple" is used twice.
Example 3
- Input:
- str = "catsandog", words = ["cats", "dog", "sand", "and", "cat"]
- Output:
- false
Constraints
1 <= str.length <= 3001 <= words.length <= 100All strings are lowercase letters.