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
struct TrieNode {
  vector<shared_ptr<TrieNode>> children;
  bool isWord = false;
  TrieNode() : children(26) {}
};

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

  bool query(char letter) {
    letters += letter;
    shared_ptr<TrieNode> node = root;

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

    return false;
  }

 private:
  shared_ptr<TrieNode> root = make_shared<TrieNode>();
  string letters;

  void insert(const string& word) {
    shared_ptr<TrieNode> node = root;
    for (int i = word.length() - 1; i >= 0; --i) {
      const int index = word[i] - 'a';
      if (node->children[index] == nullptr)
        node->children[index] = make_shared<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
33
from dataclasses import dataclass


@dataclass
class TrieNode:
  children: dict[str, TrieNode]
  isWord: bool


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):
      node = node.children.setdefault(c, TrieNode())
    node.isWord = True