TIC
The Interns Company

Implement Trie (Prefix Tree)

MediumAcceptance: 56.8%

Problem Statement

A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. Implement the Trie class: Trie() Initializes the trie object. void insert(String word) Inserts the string word into the trie. boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise. boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.

Constraints:

  • 1 <= word.length, prefix.length <= 2000
  • word and prefix consist only of lowercase English letters.
  • At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.

Input Format:

  • Commands to execute operations on Trie data structure.

Output Format:

  • Results of operations performed on the Trie data structure.

Examples:

Example 1:

Input:

Input
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]

Output:

Output
[null, null, true, false, true, null, true]

Explanation:

Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // return True trie.search("app"); // return False trie.startsWith("app"); // return True trie.insert("app"); trie.search("app"); // return True

Solutions

Standard Trie Implementation with Array

Time: O(L) for all operations where L is the length of the word/prefix
Space: O(N * 26) where N is the total number of nodes in the trie

Implement a trie using an array of 26 child nodes for each trie node, where each position in the array corresponds to a lowercase letter.

HashMap Implementation

Time: O(L) for all operations where L is the length of the word/prefix
Space: O(N * K) where N is the total number of nodes and K is the average number of children per node

Implement a trie using a HashMap for each node's children instead of an array. This approach is more flexible for different character sets but may have slightly more overhead.

Algorithm Walkthrough

Example input: nums = []

Step-by-step execution:

  1. Initialize a new Trie with an empty root node
  2. Insert "apple":
  3. Create nodes for 'a', 'p', 'p', 'l', 'e'
  4. Mark the node for 'e' as the end of a word
  5. Search for "apple":
  6. Follow path 'a' -> 'p' -> 'p' -> 'l' -> 'e'
  7. The node at 'e' is marked as the end of a word
  8. Return true
  9. Search for "app":
  10. Follow path 'a' -> 'p' -> 'p'
  11. The node at 'p' is not marked as the end of a word
  12. Return false
  13. StartsWith "app":
  14. Follow path 'a' -> 'p' -> 'p'
  15. The prefix path exists
  16. Return true
  17. Insert "app":
  18. Follow existing path 'a' -> 'p' -> 'p'
  19. Mark the node at 'p' as the end of a word
  20. Search for "app":
  21. Follow path 'a' -> 'p' -> 'p'
  22. The node at 'p' is now marked as the end of a word
  23. Return true

Hints

Hint 1
Think about how to represent a trie node with children for each possible character.
Hint 2
Each node should track whether it represents the end of a word.
Hint 3
For the startsWith method, you only need to check if the prefix path exists, not if it's a complete word.

Video Tutorial

Video tutorials can be a great way to understand algorithms visually

Visualization

Visualize the trie data structure and how words are stored and searched in it.

Key visualization elements:

  • current node
  • inserted word path
  • search path
  • matching prefix

Implementation Notes

Tries are fundamental data structures for fast prefix lookups. They're commonly used in applications like autocomplete, spell checking, and IP routing.