Advertisement

Implement Trie (Prefix Tree) - LeetCode 208 Solution

Implement Trie (Prefix Tree) - Complete Solution Guide

Implement Trie (Prefix Tree) is LeetCode problem 208, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 startsWit

Detailed Explanation

The problem requires implementing a Trie data structure, also known as a prefix tree. This data structure is specifically designed for efficient storage and retrieval of strings based on their prefixes. We need to implement three key functionalities: `insert(word)` to add a word to the Trie, `search(word)` to check if a word exists in the Trie, and `startsWith(prefix)` to determine if any word in the Trie starts with a given prefix. The input consists of a series of operations (insert, search, startsWith) performed on strings composed of lowercase English letters, and we must correctly return the results of search and startsWith operations.

Solution Approach

The solution implements a Trie using nested hash maps (or an array of node pointers in C). Each node in the Trie represents a character. The `insert` operation traverses the Trie, creating new nodes for characters that are not already present. The `search` operation traverses the Trie to find the complete word, returning true only if the path exists and the final node is marked as the end of a word. The `startsWith` operation traverses the Trie to find the prefix, returning true if the path exists, regardless of whether the final node is marked as the end of a word. The python, java and cpp implementations make use of hash maps to store node children. The C implementation makes use of a fixed-size array to do the same, but is limited to lowercase alphabet.

Step-by-Step Algorithm

  1. Step 1: Initialization: Create a Trie object with a root node. This root node is typically an empty hash map (or array in C) that will hold references to child nodes.
  2. Step 2: Insert: For each character in the word to be inserted, check if the current node has a child node corresponding to that character. If not, create a new node for that character and add it as a child of the current node. Move to the child node and repeat for the next character. After processing all characters, mark the final node as the end of a word.
  3. Step 3: Search: For each character in the word to be searched, check if the current node has a child node corresponding to that character. If not, return false (word not found). Move to the child node and repeat for the next character. After processing all characters, check if the final node is marked as the end of a word. If yes, return true (word found), otherwise, return false.
  4. Step 4: StartsWith: For each character in the prefix, check if the current node has a child node corresponding to that character. If not, return false (prefix not found). Move to the child node and repeat for the next character. After processing all characters, return true (prefix found), regardless of whether the final node is marked as the end of a word.

Key Insights

  • Insight 1: A Trie is a tree-like data structure where each node represents a character, and paths from the root to a node represent prefixes or words.
  • Insight 2: Using a hash table (or array in C) to store the children of each node allows for efficient lookup of characters.
  • Insight 3: A boolean flag at each node indicates whether a complete word ends at that node, which is crucial for differentiating between prefixes and complete words in the search function.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n*m)

Topics

This problem involves: Hash Table, String, Design, Trie.

Companies

Asked at: Arista Networks, Block, Citadel, Docusign, DoorDash, Grammarly, Lyft, MakeMyTrip, Mapbox, Nvidia, Pinterest, Roblox, Samsung, ServiceNow, Snowflake, X, instabase.