LeetCode Trie Related Problems

just search trie
LC208: Implement Tree
LC211: Add and Search Word-DS design: added word only contains letters. and we have a ‘.’ as a wildcard to match any single char. Solution: this is a classic trie problem, only by looking at this problem we know it can be solved by trie for sure. just be careful of the regex.
LC212: Word Search2: Now we have a 2D array full of chars, search the words appears in that board. and all the words are defined in words[] array. so this time, we use every word in words dictionary to build a trie, and using four way dfs for every point in the board. there are few things to pay attention to: the root of trie is actually a empty trieNode, so each time we check cur.children[]. and we should use backtracking technique which means overwrite the point we current using and get back to original when four way dfs is done. and three, each time we get a valid word in board, we should mark the node.word as null in order to avoid duplicate.
LC336 Palindrome Pairs:
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Solution: this problem can be easily solved in brute force. but how does it connected to trie?
LC421 Maximum XOR of two numbers in an array: in O(n) time, find a pair in an array which has the maximum of ai XOR aj. this can easily be done in brute force, but how does this connected to trie? because, we know that each number can be represented as binary, and we want the bigger XOR, we need to get as many as 0-1 pair for two numbers. now all the numbers in given array is like a dictionary. and we have to use that to build a trie, and it is bitwise trie.
LC425 Word Squares: given a set of words, return all the words squares you can build from them. we don’t have to use all the words in given dictionary. I have a detailed article about this problem. using Backtracking + Trie
LC472 Concatenated Words:Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.
and this problem can be translate to the following meaning: some words in given list can concatenated into other word in the same list, and we gonna to find all the words that can be concatenated by others(we can repeatly use those words). and more short speaking, the problem needs us to find all the concatenated words. how will this have anything to do with trie? well, we using every words to construct the trie. and we tranverse each word again, if there is more than one node on a path that shows the isWord attribute is true, then we have a valid answer which is a part of final results.
LC642 Design Search Autocomplete System: a classic trie problem, we have an article about it. frankly speaking, it’s like a trie using in real life.
LC692 Top K Frequent Words: give a list of words, return the top k frequent. like all the problems that can be solved in trie, this problem seems pretty easy with using brute force. and I get it done using a simple pq. how does this problem have anything related to trie? well, if you compare LC642 and LC692 you will find many similiarity. instead of show the top 3 sentence, we need to show the top k words. amd you know, “words” and “sentence” are essentially the same. we have a detailed article about this problem.(but using trie, how can we get the top k in times?)

posted @ 2020-05-04 10:47  EvanMeetTheWorld  阅读(25)  评论(0)    收藏  举报