Coding interview problems › Dynamic Programming
🪙 Dynamic Programming Medium
Word Break
Word Break explained: decide if a string splits into dictionary words using a 1D DP over prefixes, tested Python solution, pitfalls and a live DP visual.
Also searched as: word break dp · can a string be segmented into dictionary words · word segmentation problem
Try it live Free
Coin Change (DP)
In the Coin Change lab, watch dp[] fill left to right with each cell built from earlier answers; in Word Break each cell is a yes or no built from earlier prefixes and one dictionary word.
Also useful: Trie & Autocomplete
The problem
Given a string s and a list of dictionary words, decide whether s can be cut into a sequence of one or more dictionary words. A word may be used more than once.
apple + pen + apple
The key insight
It is coin change with words as coins. Let ok[i] mean "the first i characters can be segmented". The prefix of length i works if some earlier cut j works and s[j:i] is a dictionary word. Fill ok from left to right.
Step-by-step approach
- Put the words in a set for O(1) lookups and note the longest word length.
- Create
okof sizen + 1withok[0] = True(the empty prefix). - For each end
ifrom 1 ton, look back at start pointsjno further than the longest word. - If
ok[j]is true ands[j:i]is in the set, markok[i] = Trueand stop looking. - Return
ok[n].
Python solution
def word_break(s, word_dict):
words = set(word_dict)
longest = max(map(len, words), default=0)
n = len(s)
ok = [True] + [False] * n # ok[i]: s[:i] can be segmented
for i in range(1, n + 1):
for j in range(max(0, i - longest), i):
if ok[j] and s[j:i] in words:
ok[i] = True
break
return ok[n]Complexity
For each of n end positions we try up to L start positions (L = longest word), and each slice and hash costs up to O(L).
Common mistakes
- Greedily matching the longest (or shortest) word first. "catsandog" style inputs need backtracking that DP handles automatically.
- Plain recursion without memo, which times out on inputs like
"aaaa...ab"with words["a", "aa", "aaa"]. - Checking the dictionary as a list instead of a set, adding an O(number of words) factor to every lookup.
Frequently asked questions
What is the time and space complexity of Word Break?
With the longest-word bound, O(n × L²) time, where L is the longest dictionary word, and O(n) space for the DP array plus the word set.
How do I return all possible sentences (Word Break II)?
Use DFS with memoization keyed by start index, returning the list of sentences for each suffix. The output itself can be exponential, so no algorithm is polynomial in the worst case.
Can a trie help in Word Break?
Yes. From each reachable start index, walk the trie along the string and mark every position where a word ends. This avoids building substrings and is useful when the dictionary is large.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Dynamic Programming Coin Change (DP) lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.