Word Break II
Problem Statement
You are given a string s and a list of words wordDict. Insert spaces into s so that every piece between spaces is a word from the list. A word can be used more than once. Return every sentence you can make this way. The sentences can be returned in any order.
A string s, and wordDict, a list of distinct words.
A list of every sentence that can be made from s, where a sentence is the words joined by single spaces. If there is none, return an empty list.
The first cut can be after cats, then and and dog follow. Or it can be after cat, then sand and dog follow.
The word apple is used twice in the first sentence. A word can be reused, so this is allowed.
Every route gets stuck. For example, cat and sand leave og, which is not a word.
- • 1 ≤ s.length ≤ 20
- • 1 ≤ wordDict.length ≤ 1000
- • 1 ≤ wordDict[i].length ≤ 10
- • `s` and `wordDict[i]` consist of lowercase English letters.
- • All the strings in `wordDict` are unique.
- • The input is chosen so that the answer is at most 10^5 characters long.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try every dictionary word at each position and recurse on the rest, with no memo
Store the sentences for each start index, so each suffix is solved once
From position start, try every word that matches s at start. Add it to the sentence and solve the rest from the next position. There is no memo, so the same suffix is solved again on every route that reaches it. When the position reaches the end of s, the sentence is complete.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.