Word Ladder — BFS Shortest Transformation
Find minimum transformations from beginWord to endWord changing one letter at a time (each intermediate must be in wordList). Classic BFS on word states.
webcoderspeed.com
22 articles
Find minimum transformations from beginWord to endWord changing one letter at a time (each intermediate must be in wordList). Classic BFS on word states.
Find the most common word not in a banned list by normalizing input and using a frequency counter.
Find all index pairs forming palindromes by checking prefix/suffix splits against a reverse-word map.
Remove adjacent pairs of same letters with different cases using a stack to build the result character by character.
Decode a string with nested encodings like 3[a2[bc]] using two stacks: one for counts and one for partial strings.
Find the shortest word transformation sequence using BFS where each step changes exactly one character.
Remove k adjacent identical characters repeatedly using a stack that tracks (character, count) pairs.
Implement KMP string search: build the failure function (LPS array) in O(m) then search in O(n). Never re-examines matched characters unlike naive search.
The Z-algorithm computes Z[i] = length of the longest substring starting at i that matches a prefix of the string. O(n) time, elegant and powerful for pattern search.
Rabin-Karp uses a rolling polynomial hash to match patterns in O(n+m) expected time. The key is updating the hash in O(1) as the window slides.
Find the longest palindromic substring in O(n) using Manacher's algorithm. Expands palindromes using a mirror trick to avoid redundant work.
Core string DP patterns: edit distance (Wagner-Fischer), longest common subsequence, interleaving strings, and regular expression matching.
Group anagrams, find anagram indices, and count anagram occurrences. Three approaches: sort key O(nk log k), frequency tuple O(nk), and sliding window O(n+k).
Design an encode/decode scheme for a list of strings. Length-prefix encoding (4-byte header per string) is collision-proof unlike delimiter-based approaches.
Add minimum characters to the front of a string to make it a palindrome. KMP trick: concatenate s + '#' + rev(s), find the LPS value at the last position.
Implement run-length encoding, string compression in-place, and decode compressed strings. Tests two-pointer technique and string manipulation.
High-frequency string problems from Meta and Google interviews: valid parentheses, longest substring without repeating characters, zigzag conversion, and string to integer.
Find the longest substring with at most k distinct characters using a sliding window with a character frequency map. O(n) time.
Generate all combinations of well-formed parentheses of given n pairs using backtracking. Track open and close counts to prune invalid branches early.
Return all ways to segment a string into dictionary words. Combines DP validity check with DFS+memo backtracking to avoid TLE on valid inputs.
Merge accounts sharing common email addresses using Union-Find. Group emails by owner, merge accounts with shared emails, sort each merged group.
Design a log storage system that retrieves log IDs within a timestamp range at a specified granularity (Year, Month, Day, Hour, Minute, Second).