Trie
题解
164 Maximum Gap
- Given a number array, find maximum gap in sorted array.
- O(32n) time with trie.
- O(n) time by pigeon hole principle. Make buckets i.e. at least one bucket is empty, then the empty bucket(s) must be maximum adjacent. Also maintain min/max for each bucket.
208 Implement Trie (Prefix Tree)
|
|
211 Add and Search Word - Data structure design
- Given word dict, support look-up supporting dot wildcard. Trie with DFS.
212 Word Search II
- Given a word list, find all words appearing in the grid.
- Use Trie to build a dict, then DFS.
336 Palindrome Pairs
- Given a word list, find all word pairs that can be combined into a palindrome.
- O(nk^2) time, using trie to find the other end of a palindrome.
- Use hashset to find also works.
411 Minimum Unique Word Abbreviation
- Given a word and a dictionary, find the smallest abbr. of the word (“apple”->“a4”, etc.) that doesn’t conflict with the dictionary. A number’s length is 1.
- O(mn2^m) time, where n2^m <= 2^20. BFS with trie. BFS all nodes when in number region.
421 Maximum XOR of Two Numbers in an Array
- O(n) time, DFS on trie.
425 Word Squares
-
Given a word list, find all word squares constructable. A word square is like
ball area lead lady -
Backtracking with validation by trie.
616 Add Bold Tag in String
- Add bold tag for all words appearing in dictionary.
- KMP on all words in dictionary is OK too.
642 Design Search Autocomplete System
- Store most frequent items on Trie nodes.
677 Map Sum Pairs
- Insert(key, val), Sum(prefix)
Linked Mentions
-
No backlinks found.