Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Dynamic Programming High frequency Solution Available Practice Mode

Word Break LeetCode Solution

Tags: #String #DP #Hash Set #Memoization

Approach Summary

dp[i] = true if s[0..i) can be segmented. For each i, check all j < i where dp[j] is true and s[j..i) is a word.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Dynamic Programming Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Dynamic Programming pattern.

Framework: Dynamic Programming • Interview Prep: Tested at FAANG • Explore all Dynamic Programming problems & invariants →

How to Recognize This Pattern

  • Can string be segmented using dictionary?

Complexity Analysis

Time Complexity

O(n² × m)

Space Complexity

O(n)

Tags

String DP Hash Set Memoization

Frequently Asked Questions

What is the optimal time and space complexity for Word Break?
The optimal solution for Word Break achieves O(n² × m) runtime complexity and requires O(n) auxiliary space. Time complexity is O(n^2 * k) where n is the length of s and k is the maximum word length (for substring hashing/copying). Space complexity is O(n + m) for the DP boolean array and the hash set.
How do I identify that Word Break requires the Dynamic Programming pattern?
In technical interviews, look for key problem signals: Can string be segmented using dictionary?. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Dynamic Programming strategy.
What critical edge cases must be handled in Word Break?
Essential edge cases to verify include: Single character matching dictionary word (e.g. s = "a", wordDict = ["a"]); No segmentation possible (e.g. s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]); Overlapping prefixes (e.g. s = "cars", wordDict = ["car", "ca", "rs"]); String where dictionary words must be reused multiple times (e.g. s = "applepenapple"). Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Word Break?
Word Break is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Google, Facebook, Bloomberg.
Buy me a coffee
Daily Practice
0/0