题解

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)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class TrieNode {
public:
    TrieNode *next[26];
    bool isEnd;
    TrieNode() {
        for (int i = 0; i < 26; i++) next[i] = nullptr;
        isEnd = false;
    }
};

class Trie {
public:
    TrieNode *root;

    Trie() {
        root = new TrieNode();
    }

    void insert(string word) {
        int wordLen = word.size();

        TrieNode* p = root;
        for (int i = 0; i < wordLen; i++) {
            if (p->next[word[i] - 'a'] == nullptr) {
                p->next[word[i] - 'a'] = new TrieNode();
            }
            p = p->next[word[i] - 'a'];
        }
        p->isEnd = true;
    }

    bool search(string word) {
        int wordLen = word.size();
        TrieNode* p = root;

        for (int i = 0; i < wordLen; i++) {
            if (p->next[word[i] - 'a'] == nullptr) return false;

            p = p->next[word[i] - 'a'];
        }

        return p->isEnd;
    }

    bool startsWith(string prefix) {
        int wordLen = prefix.size();
        TrieNode* p = root;

        for (int i = 0; i < wordLen; i++) {
            if (p->next[prefix[i] - 'a'] == nullptr) return false;

            p = p->next[prefix[i] - 'a'];
        }

        return true;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */

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)