Trie

26 articles

dsa8 min read

Word Search II — Trie + DFS Backtracking on a Grid

Word Search II (LC 212) is the canonical FAANG hard combining trie data structures with grid backtracking. Build a trie from the dictionary, DFS each cell, and prune aggressively to convert an exponential brute force into a fast practical algorithm.

Read →
dsa7 min read

Word Search II — Trie + Backtracking on a Grid

LeetCode 212 — find every dictionary word hidden in a board. The optimal solution builds a trie over the words and DFS-traverses the grid once, pruning entire branches the moment the path leaves the trie. A textbook FAANG hard problem.

Read →
dsa6 min read

Replace Words — Trie for Shortest Root Replacement

LeetCode 648 — replace each word in a sentence with the shortest dictionary root that prefixes it. The trie walks one character at a time, returning the first isEnd we hit. A clean autocomplete-style problem.

Read →
dsa7 min read

Maximum XOR of Two Numbers in an Array — Binary Trie

LeetCode 421 — find the maximum XOR pair in an array of integers in O(N times 32) using a binary trie. The classic introduction to bit-trie pattern that powers competitive programming and database query optimisers.

Read →
dsa7 min read

Search Suggestions System — Trie Powered Autocomplete

LeetCode 1268 — return up to three lexicographically smallest products for every prefix of a search query. The trie autocomplete pattern that backs Google search, Amazon product search, and command palettes everywhere.

Read →
dsa7 min read

Prefix and Suffix Search — Combined Key Trie Trick

LeetCode 745 — design a structure that returns the highest-indexed word matching a given prefix and suffix. The combined-key trie inserts every (suffix#word) variant, turning a 2D query into a 1D trie walk.

Read →
dsa6 min read

Sum of Prefix Scores of Strings — Counted Trie Aggregation

LeetCode 2416 — for each word, sum the scores of all its non-empty prefixes where score = number of words sharing that prefix. The counted trie pattern aggregates O(N times L) work into O(N times L) trie nodes with a count counter.

Read →
dsa7 min read

Concatenated Words — Trie + Word Break DP

LeetCode 472 — find every word that can be formed by concatenating two or more shorter words from the same list. Trie accelerates the prefix-membership test inside a Word Break DP, turning O(2^L) brute force into O(N times L^2).

Read →
dsa7 min read

Short Encoding of Words — Reverse Trie Suffix Deduplication

LeetCode 820 — encode a list of words into the shortest reference string where each word appears as a suffix terminated by #. Build a trie of reversed words; only words at trie leaves contribute their length plus one to the answer.

Read →
dsa6 min read

Map Sum Pairs — Trie with Cumulative Sum Propagation

LeetCode 677 — design a structure supporting insert(key, value) and sum(prefix) returning the total of values for all keys with that prefix. The key trick is propagating delta sums along the trie path so prefix queries become a single O(L) walk.

Read →
dsa8 min read

Maximum XOR With an Element From Array — Offline Binary Trie

LeetCode 1707 — for each query (xi, mi), find max xi XOR nums[j] where nums[j] does not exceed mi. Sort queries by mi, sort nums, insert lazily into a binary trie, answer each query in O(32). The offline-trie pattern unlocks bounded XOR queries.

Read →
dsa7 min read

Find the Length of the Longest Common Prefix — Digit Trie

LeetCode 3043 — find the longest common prefix length between any number in arr1 and any number in arr2 (compared as digit strings). A digit trie of arr1 makes each arr2 lookup O(D) where D is the number of digits.

Read →
dsa6 min read

Count Distinct Substrings — Suffix Trie Node Counting

Count the number of distinct substrings of a string. The elegant trick: every substring is a prefix of some suffix, so a suffix trie has exactly one node per distinct non-empty substring. Count nodes during insertion in O(N^2).

Read →
dsa5 min read

Tries — Master Recap and Interview Cheatsheet

Complete Tries cheatsheet for FAANG interviews: core operations, 5 patterns, binary trie for XOR, decision guide, complexity table, and problem index covering all 20 trie problems.

Read →