Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Count Distinct Words

Triesmedium

Implement `trieInsertAndCount(words)` that inserts all words into a trie and returns the number of distinct words (duplicates count once).

Sample tests

Input: trieInsertAndCount(["cat","cat","car"])
Output: 2
Input: trieInsertAndCount([])
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Counting insertions instead of distinct end markers double-counts duplicates.

Learning resources

  • Wikipedia: Trie
Approach & explanation (try first)

Each distinct word sets exactly one end flag, so counting end flags counts distinct words.

Loading...
⌘/Ctrl + Enter

Run your code to see results.