Skip to main content
mediumBlind 75

Word Break

Given a string s and a dictionary of words Return true if s can be segmented into dictionary words The same word may be reused multiple times Example: s = "leetcode", wordDict = ["leet","code"] => true

Given a string s and a list of dictionary words wordDict, return true if s can be split into a sequence of one or more dictionary words, and false otherwise. Any word may be used more than once. Part 2 returns the fewest words in such a split, or -1 if none exists. Part 3 returns the number of distinct ways to split s, or 0 if there are none.

Examples

You write

word_break(s: str, word_dict: List[str])

  1. Example 1

    Input
    s = "leetcode", wordDict = ["leet","code"]
    returns
    true
  2. Example 2

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

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Word Breakmedium
2Part 2
3Part 3

Each locked part opens in the editor once the part before it passes.

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.