Skip to problem
←

Word Break (Trie)

Medium 0 attempts

You're given a string s and a dictionary of words wordDict. Decide whether s can be broken up into a sequence of one or more dictionary words placed back to back, where the same dictionary word may be reused as many times as needed.

Every character must be used exactly once, in its original order. Each piece must be a complete dictionary word: a prefix alone is not enough. Dictionary order does not matter, and not every dictionary word needs to be used. Return a boolean, not the segmentation.

Use a trie to look up dictionary prefixes efficiently. For s = "cars" and wordDict = ["car", "ca", "rs"], choose "ca" + "rs"; choosing "car" first would leave an unmatched "s".

Function contract

Implement wordBreak(s, wordDict).

Output: Return true if the entire string can be split into dictionary words; otherwise return false.

Example 1

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

Example 2

Input
s = "catsandog"
wordDict = ["cats","dog","sand","and","cat"]
Output
false
Constraints
  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20

Trie with reachable prefixes

Build a trie of dictionary words, marking complete words separately from intermediate prefixes. Let reachable[i] mean that the first i characters of s can be segmented; initially only reachable[0] is true.

From each reachable position, walk forward through the trie. Each terminal node makes the next string position reachable. Stop when an edge is missing. Continue past terminal nodes too: a longer word may also lead to a valid segmentation. This explores all word choices without recursive recomputation or substring creation.

javascript
function wordBreak(s, wordDict) {
  function node() {
    return { children: new Map(), terminal: false };
  }
  const root = node();
  for (const word of wordDict) {
    let current = root;
    for (let i = 0; i < word.length; i++) {
      const char = word[i];
      if (!current.children.has(char)) current.children.set(char, node());
      current = current.children.get(char);
    }
    current.terminal = true;
  }

  const reachable = Array(s.length + 1).fill(false);
  reachable[0] = true;
  for (let start = 0; start < s.length; start++) {
    if (!reachable[start]) continue;
    let current = root;
    for (let end = start; end < s.length; end++) {
      current = current.children.get(s[end]);
      if (!current) break;
      if (current.terminal) reachable[end + 1] = true;
    }
  }
  return reachable[s.length];
}

Let n be the string length, D the total number of characters in the dictionary, and L the longest dictionary word.

Time: O(D + n · min(n, L)), assuming constant-time map operations.

Space: O(D + n) for the trie and reachability array.

Loading discussion…

Saved in this browser only. Private to you.

JavaScript