Learning pathsPricingChecking sign-in…
Data structures

Tries

Share prefixes across strings.

Shared prefix paths support different terminal, branching, and ownership contracts.

Mechanism and state

Use one node per distinct prefix, character-labelled children, and explicit terminal state. Insert marks only the complete word; a path alone is not a word. Wildcard search carries (node,consumedIndex), branching a dot over existing one-letter children. Board search carries both a trie prefix and a simple coordinate path with path-local used flags. Shortest-root replacement stops at the first terminal depth while a separate scan preserves original token boundaries and spaces.

Why the pattern works

A literal edge extends exactly one matching prefix; a dot extends every permitted one-letter alternative and consumes one position. Full pattern exhaustion accepts only a terminal node. Board search preserves adjacency, no coordinate reuse, and prefix membership on each push; unmarking on pop restores availability for later paths. Clearing a found word terminal deduplicates output but keeping children preserves longer words. Root replacement visits candidate lengths in increasing order, so its first terminal is shortest.

Work through an example

After add("bad"), search("ba") is false while search("b..") is true. Empty add marks the root and changes empty-pattern search. On board ["oa","ht"], finding "o" must not remove descendants needed by "oa" and "oath". For roots ["cattle","cat","c"], token "cattle" becomes "c"; the outer cursor still resumes at the original token end. Every literal sentence space is copied, including repeated leading/trailing spaces.

Where the reasoning fails

The 26-child array assumes lowercase ASCII, with dot permitted only in wildcard patterns. When rejecting invalid additions, validate the complete string before mutating. Search may allocate query frames but must not alter the retained trie. Wildcards can explore many trie nodes and board paths remain exponential in the worst case; prefix pruning is not a linear-time guarantee. Terminal consumption is board-call-local, while coordinate visitation is path-local. Aliased board rows make in-place marker writes especially unsafe. Empty dictionary words/roots follow each API’s explicit contract.

Reconstruct the reasoning

Trace literal and wildcard queries with terminal flags, consumed depth and pending alternatives. On a board, annotate coordinate ownership separately from shared prefix nodes and explain when each is released. Then replace tokens with overlapping roots while preserving every space and the original scan boundary. Account separately for trie preprocessing, explored search states, output sorting and text-builder storage. In DSA-132, trace cars with words car, ca, rs. A prefix walk may discover multiple terminal endpoints, and only already reachable text boundaries can start valid word edges. Explain why trie prefixes without terminals do not imply segmentation.

Apply this foundation

Your study notes