-
Notifications
You must be signed in to change notification settings - Fork 0
LeetCode Trie Prefix Tree #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree)  | ||
|
|
||
| A [**trie**](https://en.wikipedia.org/wiki/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. | ||
|
|
||
| ## Example 1 | ||
|
|
||
| ```bash | ||
| Input | ||
| ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] | ||
| [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]] | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
||
| ## 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`. |
76 changes: 76 additions & 0 deletions
76
leetcode/medium/0208-implement-trie-prefix-tree/implement-trie-prefix-tree.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||
| class TrieNode { | ||||||
| children: { [key: string]: TrieNode } = {}; | ||||||
| isEndOfWord: boolean = false; | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| * @time - All operations run in O(L) time where L is the word length. This is because we process each character once. | ||||||
| * @space - Depends on the total number of characters across all words inserted, but shared prefixes help to reduce redundancy. | ||||||
| */ | ||||||
| class Trie { | ||||||
| private root: TrieNode; | ||||||
|
|
||||||
| constructor() { | ||||||
| this.root = new TrieNode(); | ||||||
| } | ||||||
|
|
||||||
| // @time - O(L) where L is the length of the word | ||||||
| insert(word: string): void { | ||||||
| // Start at root | ||||||
| let current = this.root; | ||||||
| // For each char in word... | ||||||
| for (const char of word) { | ||||||
| // If char not in current.children, add new TrieNode | ||||||
| // Each lookup/insertion is O(1) | ||||||
| if (!current.children[char]) { | ||||||
| current.children[char] = new TrieNode(); | ||||||
| } | ||||||
| // Move to the child node for this character | ||||||
| current = current.children[char]; | ||||||
| } | ||||||
| // Mark the last node as end of a word | ||||||
| current.isEndOfWord = true; | ||||||
| } | ||||||
|
|
||||||
| // @time - O(L) where L is the length of the word | ||||||
| search(word: string): boolean { | ||||||
| let current = this.root; | ||||||
| // Traverse word | ||||||
| for (const char of word) { | ||||||
| // If char not found in current.children then the word does not exists | ||||||
| // Each child being accessed is O(1) | ||||||
| if (!current.children[char]) { | ||||||
| return false; | ||||||
| } | ||||||
| // Move to the child node | ||||||
| current = current.children[char]; | ||||||
| } | ||||||
| // After traversing, check if current is end of the word | ||||||
| return current.isEndOfWord; | ||||||
| } | ||||||
|
|
||||||
| // @time - O(L) where L is the length of the prefix | ||||||
| startsWith(prefix: string): boolean { | ||||||
| // Same as search, but return true if path exists (ignore isEndOfWord) | ||||||
| let current = this.root; | ||||||
|
|
||||||
| for (const char of prefix) { | ||||||
| // Each child being accessed is O(1) | ||||||
| if (!current.children[char]) { | ||||||
| return false; | ||||||
| } | ||||||
| current = current.children[char]; | ||||||
| } | ||||||
|
|
||||||
| // Full prefix path exits | ||||||
|
||||||
| // Full prefix path exits | |
| // Full prefix path exists |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammatical error: "exists" should be "exist" to match the singular subject "the word".