15% off all plans & roadmaps Use code
See plansLeetCode 139 · Solution & editorial
Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.
Word Break is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Word Break Style) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
Determine if string can be segmented into dictionary words using DP.
The reference solution runs in O(n * L) time and O(n + D) space.
Word Break has been reported in interviews at Amazon, Apple, Bloomberg, ByteDance, eBay, Facebook, Google, Microsoft.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Word Break is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Word Break Style) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.