LeetCode Solutions

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]}|))$

			

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;
  }
};
			

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;
  }
}
			

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