Number of Distinct Islands — DFS Shape Hashing
Count islands with distinct shapes. Encode each island's DFS traversal path as a string to capture shape, store in a set.
webcoderspeed.com
6 articles
Count islands with distinct shapes. Encode each island's DFS traversal path as a string to capture shape, store in a set.
Find all duplicate subtrees by serializing each subtree and using a hash map to detect duplicates.
Sum all root-to-leaf path sums in a tree encoded as 3-digit integers using a hashmap to decode tree structure.
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.
Precompute polynomial prefix hashes to compare any two substrings in O(1). Enables O(n log n) LCP computation and O(n) duplicate detection.
Find the longest common substring between two strings. DP approach O(nm), binary search + rolling hash O((n+m) log(n+m)).