← All problems

Word Break

Medium dynamic programmingstrings LeetCode ↗
Given a string s and a dictionary of words, determine whether s can be segmented into a space-separated sequence of one or more dictionary words. The same word may be reused.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n^2), Space O(n)

Common pitfalls

  • Greedy longest-match (fails)
  • Not memoizing the recursion (exponential)

Your solution

Log in to save your code and track progress.