VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 12: Dynamic programming

Lesson 12.9 of 16 in this module Unrolled Pro

Word Break

Can a string be split into a sequence of dictionary words? From trying every split recursively (exponential when prefixes repeat), to memoising on the start position, to a table where ok[i] says whether the first i characters can be split (O(n · L) with a word-length cap and a hash set). Then returning one or all segmentations, the most likely segmentation under word probabilities, and segmenting text with no spaces, the problem a unigram tokenizer solves for every word it encodes.

about 10 min 3 Python examples 2 quick checks

Problem. Given a string s and a dictionary of words, return whether s can be segmented into a space-separated sequence of one or more dictionary words (words may be reused).

text
s = "leetcode",       dict = ["leet", "code"]                   →   True
s = "applepenapple",  dict = ["apple", "pen"]                   →   True
s = "catsandog",      dict = ["cats", "dog", "sand", "and", "cat"]   →   False

Lesson 12.2 counted segmentations of a string into tokens. Here we ask whether any exists, and the state is the same: a prefix length i is "good" if some dictionary word ends at i and the prefix before that word is good.

ML context. Splitting text without spaces into known units is what subword tokenizers do for every word: a unigram tokenizer (SentencePiece) picks the most probable segmentation of each word into vocabulary pieces with exactly this DP using probabilities (Change C). It's also how Chinese and Japanese text, hashtags (#machinelearningrocks) and URLs are split into words.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Word Break"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee