Trie

    /** Initialize your data structure here. */
    TrieNode root;
    public Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        TrieNode curt = root;
        for (char c : word.toCharArray()) {
            int id = c - 'a';
            if (curt.children[id] == null) curt.children[id] = new TrieNode();
            curt = curt.children[id];
        }
        curt.isWord = true;    
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        return find(word.toCharArray(), root, false);
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        return find(prefix.toCharArray(), root, true);
    }
    
    private boolean find(char[] chars, TrieNode curt, boolean prefix) {
        for (char c : chars) {
            int id = c - 'a';
            if (curt.children[id] == null) return false;
            curt = curt.children[id];
        }
        if (prefix) return true;
        return curt.isWord;
    }
    
    class TrieNode {
        TrieNode[] children;
        boolean isWord = false;
        TrieNode() {
            this.children = new TrieNode[26];
        }
    }

Build Trie and store index of the string in folder, the index has default value of -1.

BFS to find all parents, offer Tire root to the queue. While queue is not empty, pop nodes with index != -1, which means it is a parent folder. Check if it is children is not null and not 26 (which represent "/"), then it is another valid answer. For example, "/a/b/ca" is not a subfolder of "/a/b/c"

When building Trie, add to the word to the end of Trie node. DFS search on Trie in 4 directions. Set the visited grid to '#' to eliminate revisit, flip it back as DFS completes.

T: O(m * n * 4 * 3 ^ (L - 1)) S : O(len*k)

Added 3 sorted suggestion words to the TrieNode. Traverse Trie by searchWord, add empty arrays if reached null nodes.

Construct Trie Tree with words provided. When search, count the words which is already found and keep finding the next word from root, return true when total words find is >= 2.

T: build Trie: O(n * k) , search: O(n* k) S: O(n * k)

Build Trie with words on the corresponding node. Simultaneously search the Trie and the board to find the word on the node, mark the grid as '#' to indicate visited, erase the word after added to the result. (this is because you only want to find one word if there are duplicates on the grid) Check if the x, y coordinates are out of bound or visited. Add word to the result.

T: O(M(4⋅3 L−1)), where MM is the number of cells in the board and LL is the maximum length of words.

At each TrieNode, set children map and a separate map to count the corresponding word and frequency. Write a function List<String> getTop3() and use max heap to produce the top 3 words in each node.

AutocompleteSystem() takes O(k*l) time. We need to iterate over l sentences each of average length k, to create the trie for the given set of sentences. input() takes O(p+q+m *log m)time. Here, p refers to the length of the sentence

Solution1: Build & Search all words in Trie (Build: O(L), Search (n * L * 26))

Solution2: Rolling Character Matching (O(S + W * L))

Last updated

Was this helpful?