Trie Implementation and Advanced Operations

Tries Theory Hard
  • Fun Fact: The Trie data structure, also known as a prefix tree, is incredibly useful in real-world applications, especially in scenarios where we need fast and efficient retrieval of words or data
  • For instance, it is the underlying data structure powering the auto-complete feature in your smartphone keyboard or search engine
  • When you start typing, it looks for words in its trie data structure that starts with the prefix you've typed and proposes the most probable completions
  • Similarly, spell-checking tools or predictive text technology in chatbots and AI-based customer service platforms make extensive use of Tries

Implement "TRIE” data structure from scratch with the following functions.

  • Trie(): Initialize the object of this “TRIE” data structure.
  • insert(“WORD”): Insert the string “WORD” into this “TRIE” data structure.
  • countWordsEqualTo(“WORD”): Return how many times this “WORD” is present in this “TRIE”.
  • countWordsStartingWith(“PREFIX”): Return how many words are there in this “TRIE” that have the string “PREFIX” as a prefix.
  • erase(“WORD”): Delete one occurrence of the string “WORD” from the “TRIE”.

Examples:

Input : ["Trie", "insert", "countWordsEqualTo", "insert", "countWordsStartingWith", "erase", "countWordsStartingWith"]

[ [], ["apple"], ["apple"], ["app"], ["app"], ["apple"], ["app"] ]


Output : [null, null, 1, null, 2, null, 1]


Explanation :

Trie trie = new Trie()

trie.insert("apple")

trie.countWordsEqualTo("apple")  // return 1

trie.insert("app") 

trie.countWordsStartingWith("app") // return 2

trie.erase("apple")

trie.countWordsStartingWith("app")   // return 1

Input : ["Trie", "insert", "countWordsEqualTo", "insert", "erase", "countWordsStartingWith"]

[ [], ["mango"], ["apple"], ["app"], ["app"], ["mango"] ]


Output : [null, null, 0, null, null, 1]


Explanation :

Trie trie = new Trie()

trie.insert("mango")

trie.countWordsEqualTo("apple")  // return 0

trie.insert("app") 

trie.erase("app")

trie.countWordsStartingWith("mango") // return 1

Input : ["Trie", "insert", "insert", "erase", "countWordsEqualTo", "insert", "countWordsStartingWith"]

[ [], ["abcde"], ["fghij"], ["abcde"], ["abcde"], ["abcde"], ["fgh"] ]

Constraints

  • 1 <= word.length , prefix.length <= 2000
  • word and prefix consist only of lowercase English letters.
  • At most 3*104 calls in total will be made to insert, countWordsEqualTo , countWordsStartingWith and erase.

Hints

  • Insertion follows a character-by-character traversal, incrementing both prefix_count for all traversed nodes and word_count at the last character node. Searching for exact words involves checking word_count, while prefix-based searches rely on prefix_count.
  • To efficiently erase a word, the word count is decremented at the last character node, and prefix_count values are reduced along the path. If word_count reaches zero but the prefix is still used by other words, the structure remains intact. However, if no words depend on a node, it can be deleted to free memory.

Company Tags

Docker Byju's Rockstar Games Instacart Freshworks Pinterest HCL Technologies Cerner Alibaba Riot Games Robinhood OYO Rooms Boston Consulting Group Databricks Micron Technology PwC Texas Instruments Activision Blizzard American Express Siemens Healthineers DoorDash Bloomberg Splunk Morgan Stanley Snowflake Google Microsoft Amazon Meta Apple Netflix Adobe