Implement Trie II (Prefix Tree)

The canonical "Implement Trie II (Prefix Tree)" problem defines a trie that supports insert, countWordsEqualTo, countWordsStartingWith, and erase, including duplicate word counts.

This repository uses a functional command-simulation contract. solve(arg1) receives an array of command strings and returns the sum of all query results.

Supported commands:

  • insert:<word> inserts word
  • erase:<word> removes one occurrence if present
  • equal:<word> contributes the number of stored copies equal to word
  • prefix:<prefix> contributes the number of stored words starting with prefix

Input: arg1 = ["insert:apple","insert:app","prefix:app","equal:apple","erase:app"]

Output: 3

  • arg1 follows the functional execution schema
  • Commands are well-formed under the fixture contract
  • Words and prefixes consist of lowercase English letters unless otherwise specified by fixtures
  • Erasing a word not present leaves state unchanged

Related Problems

  • implement-trie-prefix-tree

Companies Asked

  • Google
  • Amazon
  • Microsoft
  • Meta
  • Twitter
Examples
Input: []
Output: 0
Hints

Implement Trie II (Prefix Tree)

The canonical "Implement Trie II (Prefix Tree)" problem defines a trie that supports `insert`, `countWordsEqualTo`, `countWordsStartingWith`, and `erase`, including duplicate word counts.