Skip to content

1032. Stream of Characters 👍

  • Time: Constructor: $O(\Sigma |\texttt{words[i]}|)$, query(letter: chr): $O(\max(|\texttt{words[i]}|))$
  • Space: Constructor: $O(\Sigma |\texttt{words[i]}|)$, query(letter: chr): $O(\max(|\texttt{words[i]}|))$
 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
struct TrieNode {
  vector<TrieNode*> children;
  bool isWord = false;
  TrieNode() : children(26) {}
  ~TrieNode() {
    for (TrieNode* child : children)
      delete child;
  }
};

class StreamChecker {
 public:
  StreamChecker(vector<string>& words) {
    for (const string& word : words)
      insert(word);
  }

  bool query(char letter) {
    letters += letter;
    TrieNode* node = &root;

    for (int i = letters.length() - 1; i >= 0; --i) {
      const int index = letters[i] - 'a';
      if (!node->children[index])
        return false;
      node = node->children[index];
      if (node->isWord)
        return true;
    }

    return false;
  }

 private:
  TrieNode root;
  string letters;

  void insert(const string& word) {
    TrieNode* node = &root;
    for (int i = word.length() - 1; i >= 0; --i) {
      const int index = word[i] - 'a';
      if (!node->children[index])
        node->children[index] = new TrieNode;
      node = node->children[index];
    }
    node->isWord = true;
  }
};
 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
class TrieNode {
  public TrieNode[] children = new TrieNode[26];
  public boolean isWord = false;
}

class StreamChecker {
  public StreamChecker(String[] words) {
    for (final String word : words)
      insert(word);
  }

  public boolean query(char letter) {
    letters.append(letter);
    TrieNode node = root;

    for (int i = letters.length() - 1; i >= 0; --i) {
      final int index = letters.charAt(i) - 'a';
      if (node.children[index] == null)
        return false;
      node = node.children[index];
      if (node.isWord)
        return true;
    }

    return false;
  }

  private TrieNode root = new TrieNode();
  private StringBuilder letters = new StringBuilder();

  private void insert(final String word) {
    TrieNode node = root;
    for (int i = word.length() - 1; i >= 0; --i) {
      final int index = word.charAt(i) - 'a';
      if (node.children[index] == null)
        node.children[index] = new TrieNode();
      node = node.children[index];
    }
    node.isWord = true;
  }
}
 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
class TrieNode:
  def __init__(self):
    self.children: Dict[str, TrieNode] = defaultdict(TrieNode)
    self.isWord = False


class StreamChecker:
  def __init__(self, words: List[str]):
    self.root = TrieNode()
    self.letters = []

    for word in words:
      self._insert(word)

  def query(self, letter: str) -> bool:
    self.letters.append(letter)
    node = self.root
    for c in reversed(self.letters):
      if c not in node.children:
        return False
      node = node.children[c]
      if node.isWord:
        return True
    return False

  def _insert(self, word: str) -> None:
    node = self.root
    for c in reversed(word):
      if c not in node.children:
        node.children[c] = TrieNode()
      node = node.children[c]
    node.isWord = True
Back to top