InterviewPrepKit

Home / Coding / Algorithm & Data Structure / 1-D Dynamic Programming

Word Break

medium Original ↗ 00:00

Problem

You are given a string s and a list of strings wordDict (a dictionary of allowed words, no duplicates). Decide whether s can be cut into a sequence of one or more pieces such that every piece is a word in wordDict. Words from the dictionary may be reused any number of times, and not every dictionary word has to be used. Return True if such a segmentation exists, otherwise False.

Examples

  • s = "leetcode", wordDict = ["leet", "code"]True — split as "leet" + "code".
  • s = "applepenapple", wordDict = ["apple", "pen"]True — split as "apple" + "pen" + "apple"; "apple" is reused.
  • s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]False — every prefix decomposition leaves a suffix ("og", "andog", …) that cannot be completed.

Constraints

  • 1 <= len(s) <= 300
  • 1 <= len(wordDict) <= 1000
  • 1 <= len(word) <= 20 for each dictionary word
  • All strings are lowercase English letters.
  • The 300 length is small, but a naive “try every split” recursion explores exponentially many segmentations. The bound is chosen so that an O(n²) DP passes while pure recursion times out.

Think about it first

Hint 1 Think about prefixes. If `s` is segmentable and its first word is `w`, then the remainder `s` with `w` stripped off the front must itself be segmentable. This "solve the smaller suffix" structure is what makes it a DP.
Hint 2 Define a boolean answer for each prefix length: can `s[:i]` be broken into dictionary words? `s[:i]` is breakable when there is some split point `j < i` where `s[:j]` is breakable AND the final chunk `s[j:i]` is a dictionary word. The empty prefix is breakable by definition.
Hint 3 Put the dictionary in a `set` for O(1) membership tests, then fill a `dp` array of size `n+1` from left to right: `dp[0] = True`, and `dp[i] = any(dp[j] and s[j:i] in words for j in range(i))`. The answer is `dp[n]`. You only ever need to test chunk lengths up to the longest dictionary word.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug