Tutorialspoint
Problem
Solution
Submissions

Prefix Tree

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a C program to implement a Trie (prefix tree) data structure. A trie is a tree-like data structure that stores a dynamic set of strings, where the keys are usually strings. Implement the Trie class with insert, search, and startsWith operations.

Example 1
  • Input:
     Operations: ["insert", "search", "search", "startsWith", "insert", "search"]
     Arguments: [["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
  • Output: [null, true, false, true, null, true]
  • Explanation:
    • Insert "apple" into the trie. Search for "apple" - returns true (word exists).
    • Search for "app" - returns false (prefix exists but not as complete word).
    • Check if any word starts with "app" - returns true.
    • Insert "app" as a complete word. Search for "app" - returns true (now exists as complete word).
Example 2
  • Input:
     Operations: ["insert", "insert", "search", "search", "startsWith"]
     Arguments: [["hello"], ["help"], ["hell"], ["hello"], ["hel"]]
  • Output: [null, null, false, true, true]
  • Explanation:
    • Insert "hello" into the trie. Insert "help" into the trie.
    • Search for "hell" - returns false (prefix exists but not complete word).
    • Search for "hello" - returns true (complete word exists).
    • Check if any word starts with "hel" - returns true.
Constraints
  • 1 ≤ word.length, prefix.length ≤ 2000
  • word and prefix consist only of lowercase English letters
  • At most 3 * 10^4 calls will be made to insert, search, and startsWith
  • Time Complexity: O(m) for all operations where m is key length
  • Space Complexity: O(ALPHABET_SIZE * N * M) where N is number of keys and M is average length
Trie Goldman SachsPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Create a TrieNode structure with children array and isEndOfWord flag.
  • Use an array of size 26 for each node to store child pointers (a-z).
  • For insert: traverse the trie creating nodes as needed, mark last node as end of word.
  • For search: traverse the trie following the word path, check if last node is end of word.
  • For startsWith: traverse the trie following the prefix path, return true if path exists.
  • Use character arithmetic (ch - 'a') to get array index for each character.

Steps to solve by this approach:

 Step 1: Define TrieNode structure with children array of size 26 and isEndOfWord boolean.

 Step 2: Implement createNode function to allocate memory and initialize all children to NULL.
 Step 3: For insert operation, traverse character by character creating nodes as needed.
 Step 4: Mark the last node as end of word by setting isEndOfWord to true.
 Step 5: For search operation, traverse the path and check if final node is end of word.
 Step 6: For startsWith operation, just traverse the prefix path without checking isEndOfWord.
 Step 7: Use character arithmetic (ch - 'a') to convert characters to array indices.

Submitted Code :