Return true if s can be split into a sequence of one or more words from wordDict. Words may be reused.
Treat each index as a node with an edge i → i + len(w) when word w matches at i. Is index n reachable from 0?
1function wordBreak(s: string, wordDict: string[]): boolean {2const queue = [0], seen = new Set([0]);3while (queue.length) {4const i = queue.shift()!;5for (const w of wordDict) {6if (!s.startsWith(w, i)) continue;7const next = i + w.length;8if (next === s.length) return true;9if (!seen.has(next)) { seen.add(next); queue.push(next); }10}11}12return false;13}
Start from index 0.
Space: play/pause · ←/→: step