Skip to main content

68. Word Break

mediumAsked at Reddit

Determine if a string can be segmented into words from a dictionary. Reddit uses this DP problem to test the prefix-suffix decomposition pattern — the same shape used when tokenizing usernames into recognized subreddit references during mention detection.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit on-site DP medium.
  • Blind (2025-10)Reported on Reddit comments-team rounds.

Problem

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words. Note that the same word in the dictionary may be reused multiple times in the segmentation.

Constraints

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • s and wordDict[i] consist of only lowercase English letters.
  • All the strings of wordDict are unique.

Examples

Example 1

Input
s = "leetcode", wordDict = ["leet","code"]
Output
true

Example 2

Input
s = "applepenapple", wordDict = ["apple","pen"]
Output
true

Example 3

Input
s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output
false

Approaches

1. Recursion no memo

For each prefix of s in dict, recurse on the suffix.

Time
O(2^n)
Space
O(n)
// Anti-pattern: exponential branching without memo.

Tradeoff: TLE.

2. DP — dp[i] = s[0..i) breakable (optimal)

dp[i] true if some j < i has dp[j] && s[j..i] in dict.

Time
O(n^2 * k)
Space
O(n)
function wordBreak(s, wordDict) {
  const set = new Set(wordDict);
  const dp = new Array(s.length + 1).fill(false);
  dp[0] = true;
  for (let i = 1; i <= s.length; i++) {
    for (let j = 0; j < i; j++) {
      if (dp[j] && set.has(s.slice(j, i))) {
        dp[i] = true;
        break;
      }
    }
  }
  return dp[s.length];
}

Tradeoff: O(n^2) DP; slice/hash lookup is O(k) where k is max word length.

Reddit-specific tips

Reddit interviewers expect the DP. Bonus signal: bound the inner loop to j >= i - maxWordLen to skip impossibly long prefixes. Mention Trie-based alternative for very large dictionaries.

Common mistakes

  • Set lookup for an array (use Set, not Array.includes — O(1) vs. O(k)).
  • Off-by-one on dp[s.length] (dp has length n+1).
  • Not breaking the inner loop after dp[i] = true (small optimization).

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Word Break II (LC 140) — return all segmentations.
  • Concatenated words (LC 472).
  • Add bold tags (LC 616).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why dp[0] = true?

The empty prefix is trivially segmentable (no words needed).

Trie optimization?

Walk s; at each position descend a Trie. Avoids the inner j loop.

Practice these live with InterviewChamp.AI

Drill Word Break and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →