Browse Curriculum
FAANGPrep Sprint
Medium

Word Break

Given a string s and a dictionary of strings, return true if s can be segmented into a space-separated sequence of dictionary words.

Problem Understanding

We need to check if the entire string can be built using dictionary words. Words can be reused.

Example: s = "leetcode", dict = ["leet", "code"]

  • "leet" + "code" -> Valid
  • Output: true

Algorithm Strategy

We define dp[i] as a boolean: Is prefix s[0...i-1] valid?

Recurrence:
dp[i] = true IF exists some j < i such that:

  1. dp[j] is true (prefix ending at j is valid)
  2. s[j...i] is in the dictionary.

Base Case: dp[0] = true (empty string).

Interactive Visualization

Step 1 / 1

Initializing...

1x

Watch how we check possible split points j. If dp[j] is valid AND the substring from j to i is in the dict, then dp[i] becomes valid.

Click 'Randomize' to test different strings.

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.