Back to Practice
#0269

Word Break

MediumDSA28 min20 XP

Problem

Given s and word_dict, return True if s can be segmented into one or more dictionary words.

Why This Matters

Word Break is the classic 1D DP problem where each state asks whether a prefix is solvable.

Function Signature

def word_break(s, word_dict):

Examples

Example 1
Inputs = "leetcode", word_dict = ["leet", "code"]
OutputTrue

leetcode can be split as leet + code.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Can split
Input"leetcode", ["leet", "code"]
ExpectedTrue

Both pieces are dictionary words.

Cannot split
Input"catsandog"
ExpectedFalse

No sequence of dictionary words consumes the whole string.

Hidden Test Categories
reused wordssingle-character wordswhole string in dictionary