Word Break
You get a string and a list of words. Can the string be cut into pieces so that every piece is a word from the list? A word may be used many times. Our example: "seashell" with the words sea, seas, hell — yes, seas + hell.
- input
s: a string;wordDict: a list of different words.- returns
trueifscan be cut into words ofwordDict, each of them used any number of times;falseif not.- constraints
- 1 ≤ s.length ≤ 200
- 1 ≤ wordDict.length ≤ 1000
- 1 ≤ wordDict[i].length ≤ 20
- s, wordDict[i]: lowercase English letters
- the words of wordDict are all different
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
function wordBreak(s, wordDict) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more dynamic programming (dp) problems
- 70Climbing StairsEasysolve
- 121Best Time to Buy and Sell StockEasysolve
- 198House RobberMediumsolve
- 322Coin ChangeMediumsolve
- 300Longest Increasing SubsequenceMediumsolve
- 62Unique PathsMediumsolve
- 1143Longest Common SubsequenceMediumsolve
- 53Maximum SubarrayMediumsolve
- 918Maximum Sum Circular SubarrayMediumsolve
- 120TriangleMediumsolve
- 64Minimum Path SumMediumsolve
- 63Unique Paths IIMediumsolve
- 97Interleaving StringMediumsolve
- 72Edit DistanceMediumsolve
- 221Maximal SquareMediumsolve
- 123Best Time to Buy and Sell Stock IIIHardsolve
- 188Best Time to Buy and Sell Stock IVHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.