Word Break
Medium Tries AI round
s is a string of lowercase letters, and wordDict is a list of different words, also of lowercase letters.
To cut `s` into words is to split it into pieces, in order and with nothing left over, so that every piece is a word of wordDict. A word may be used any number of times, or not at all.
Return true if s can be cut into words, and false if it cannot.
Example 1
Input: s = "bookshelfbook", wordDict = ["book","shelf","books","elf"]
Output: true
Cut it as book, shelf and book again: every piece is a word, and a word may be used twice. Starting with books instead leaves helfbook, which no word begins, but one way is enough.
Example 2
Input: s = "nightowl", wordDict = ["night","nigh","tow","owls"]
Output: false
Example 3
Input: s = "ababab", wordDict = ["ab","aba","bab"]
Output: true
Constraints
1 ≤ s.length ≤ 4001 ≤ wordDict.length ≤ 10001 ≤ wordDict[i].length ≤ 20sandwordDict[i]are lowercase English letters- The words in
wordDictare all different
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Tries
- Implement Trie (Prefix Tree)AI round · Medium
- Word Search IIAI round · Hard
- Longest Common PrefixEasy
- Design Add and Search Words Data StructureMedium
- Top K Frequent WordsMedium