Strings & Compression
String algorithms exploit prefix overlap, rolling polynomial arithmetic, and frequency-based prefix codes to search, index, and compress textual data efficiently. Tries store shared prefixes across dictionaries; Rabin-Karp searches patterns via rolling hashes; Huffman coding constructs optimal prefix-free variable-length codes.
Key Invariants & Rules
- In a Trie, all descendants of a node share the common string prefix associated with that node.
- Terminal trie flags indicate valid dictionary words independent of tree depth.
- Rabin-Karp updates its rolling window hash in O(1) arithmetic: hash = (hash - leading·B^(M-1))·B + trailing.
- Huffman trees are prefix-free: no character code is a prefix of any other character code.
- Huffman PriorityQueue merges the two least-frequent subtrees at each step until a single root remains.
Must-Know Algorithms
Tree of character transitions finding prefix completions in time independent of dictionary size.
Rolling hash algorithm comparing text windows in constant time.
Greedy optimal prefix code minimizing expected bit length based on character frequency.
Learning Path (4 Lessons)
Self-Check & Concept Review
1. Why does Rabin-Karp check full string equality when a hash match occurs?
2. What ensures that Huffman codes can be uniquely decoded without delimiter characters between symbols?
3. In an R-way Trie with alphabet size R, what is the search time for a key of length L?
4. How does rolling hash subtraction maintain positive modulo values in Java integer arithmetic?
5. In Huffman coding, which symbols receive the shortest bit sequences?