
Problem
Solution
Submissions
Trie (Prefix Tree)
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a Java program to implement a Trie (Prefix Tree). A trie is a tree-like data structure used to store a dynamic set of strings, where the keys are usually strings. The Trie should support the following operations:
- `insert(word)`: Inserts a word into the trie.
- `search(word)`: Returns true if the word is in the trie, false otherwise.
- `startsWith(prefix)`: Returns true if there is any word in the trie that starts with the given prefix, false otherwise.
Example 1
- Input:
- ["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
- [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
- Output: [null, null, true, false, true, null, true]
- Explanation:
- Initialize a new Trie object
- Insert the word "apple" into the trie
- Search for "apple" - returns true as it was inserted
- Search for "app" - returns false as it wasn't inserted
- Check if any word starts with "app" - returns true as "apple" starts with "app"
- Insert the word "app" into the trie
- Search for "app" - returns true as it was inserted
Example 2
- Input:
- ["Trie", "insert", "insert", "insert", "startsWith", "search", "search", "startsWith"]
- [[], ["hello"], ["world"], ["hi"], ["h"], ["hello"], ["hey"], ["world"]]
- Output: [null, null, null, null, true, true, false, false]
- Explanation:
- Initialize a new Trie object
- Insert the word "hello" into the trie
- Insert the word "world" into the trie
- Insert the word "hi" into the trie
- Check if any word starts with "h" - returns true as "hello" and "hi" start with "h"
- Search for "hello" - returns true as it was inserted
- Search for "hey" - returns false as it wasn't inserted
- Check if any word starts with "world" - returns false as no word starts with "world" (although "world" itself exists)
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
- You may assume all inputs are valid
- Each word consists only of lowercase English letters
- Time Complexity:
- insert: O(m), where m is the length of the word
- search: O(m), where m is the length of the word
- startsWith: O(m), where m is the length of the prefix
- Space Complexity: O(T), where T is the total number of characters in all inserted words
Editorial
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. | ||||
Solution Hints
- Use a tree-like data structure where each node represents a character
- Each node should have a boolean flag to mark the end of a word
- Each node should have children nodes for all possible next characters
- Use a TrieNode class to represent each node in the trie
- For insert, navigate or create nodes for each character and mark the last node as end of word
- For search, navigate through nodes for each character and check if the last node is marked as end of word
- For startsWith, navigate through nodes for each character but don't need to check if the last node is end of word