Skip to content

Word Break

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

Note that the same word in the dictionary may be reused multiple times in the segmentation.

swordDictOutputExplanation
"leetcode"["leet","code"]trueCan be segmented as “leet code”
"applepenapple"["apple","pen"]trueCan be segmented as “apple pen apple”
"catsandog"["cat","cats","and","sand","dog"]falseCannot be segmented
  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • s and wordDict[i] consist of only lowercase English letters
  • All strings in wordDict are unique
ApproachTimeSpaceDescription
DP with SetO(n²)O(n + m)Simple hash set lookup, checks all substrings
DP with TrieO(n*k)O(n + T)Trie-based prefix matching, faster for overlapping words

where n = len(s), m = len(wordDict), k = max word length, T = total chars in dict

  • Pick DP with Set if you want the simplest implementation.
  • Pick DP with Trie if you want to optimize for large dictionaries.
Simpler Logic
DP with Set
O(n²) time · O(n + m) space
Optimized for Dicts
DP with Trie
O(n*k) time · O(n + T) space
★ Recommended

Use dynamic programming with a hash set for word lookup. dp[i] represents whether s[0:i] can be segmented.

For each position i, check all previous positions j where dp[j] is True, and if s[j:i] is in the dictionary, mark dp[i] as True.

⏱ Time O(n²) All substrings checked 💾 Space O(n + m) DP array + set

Input: s = "applepenapple", wordDict = ["apple", "pen"]

is[0:i]dp[i]Explanation
0""TrueEmpty string is segmentable
5”apple”TrueFound in dictionary, dp[0] = True
8”applepen”True”pen” found, dp[5] = True
13”applepenapple”True”apple” found, dp[8] = True
function wordBreakDP(s, wordDict):
wordSet = convert wordDict to set
dp = array of size len(s) + 1, initialized to False
dp[0] = True
for i from 1 to len(s):
for j from 0 to i:
if dp[j] and s[j:i] in wordSet:
dp[i] = True
break
return dp[len(s)]
word_break_dp_set.py
from typing import List
def word_break_dp_set(s: str, word_dict: List[str]) -> bool:
"""
Determine if string can be segmented using words from dictionary.
Time Complexity: O(n²) where n = len(s)
Space Complexity: O(n) for DP array + O(m) for set where m = len(word_dict)
dp[i] = True if s[0:i] can be segmented
For each position i, check all previous positions j where dp[j] = True
and s[j:i] is in the word dictionary.
"""
word_set = set(word_dict)
dp = [False] * (len(s) + 1)
dp[0] = True # Empty string can be segmented
for i in range(1, len(s) + 1):
for j in range(i):
if dp[j] and s[j:i] in word_set:
dp[i] = True
break
return dp[len(s)]
if __name__ == "__main__":
print(word_break_dp_set("leetcode", ["leet", "code"])) # True
print(word_break_dp_set("applepenapple", ["apple", "pen"])) # True
print(word_break_dp_set("catsandog", ["cat", "cats", "and", "sand", "dog"])) # False
🎯 Interview Favourite

Build a Trie from the dictionary and use it for prefix-based word matching. For each position i, traverse the Trie backward from i to find all words ending at i.

This avoids checking all substrings; instead, we only traverse valid prefixes.

⏱ Time O(n*k) Trie traversal max k chars 💾 Space O(n + T) DP array + Trie

Input: s = "applepenapple", wordDict = ["apple", "pen"]

The Trie structure:

a
p
p
l
e (end)
n
...

For position 5 (s[0:5] = “apple”):

  • Traverse Trie with “apple”, find it marked as end → dp[5] = True

For position 8 (s[5:8] = “pen”):

  • Traverse Trie with “pen”, find it marked as end, and dp[5] = True → dp[8] = True
function wordBreakTrie(s, wordDict):
root = build Trie from wordDict
dp = array of size len(s) + 1, initialized to False
dp[0] = True
for i from 1 to len(s):
if not dp[i]:
node = root
for j from i-1 down to 0:
char = s[j]
if char not in node.children:
break
node = node.children[char]
if dp[j] and node.is_end:
dp[i] = True
break
return dp[len(s)]
word_break_dp_trie.py
from typing import List
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
def word_break_dp_trie(s: str, word_dict: List[str]) -> bool:
"""
Determine if string can be segmented using words from dictionary (Trie approach).
Time Complexity: O(n*m) where n = len(s), m = max word length
Space Complexity: O(n) for DP array + O(T) for Trie where T = total characters in dictionary
Build a Trie from the dictionary, then use DP with Trie for faster lookups.
dp[i] = True if s[0:i] can be segmented.
Instead of checking all substrings (O(n²)), we traverse the Trie (O(m)).
"""
# Build Trie
root = TrieNode()
for word in word_dict:
node = root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_end = True
dp = [False] * (len(s) + 1)
dp[0] = True
for i in range(1, len(s) + 1):
if not dp[i]:
# Try to find a word ending at position i
node = root
for j in range(i - 1, -1, -1):
char = s[j]
if char not in node.children:
break
node = node.children[char]
if dp[j] and node.is_end:
dp[i] = True
break
return dp[len(s)]
if __name__ == "__main__":
print(word_break_dp_trie("leetcode", ["leet", "code"])) # True
print(word_break_dp_trie("applepenapple", ["apple", "pen"])) # True
print(word_break_dp_trie("catsandog", ["cat", "cats", "and", "sand", "dog"])) # False
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
word_break_space_optimized.py
"""
Solution for Word Break
"""
def solve():
"""Implementation for approach 3 of Word Break"""
pass
if __name__ == "__main__":
print("Solution ready")