Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear

| Platform | Focus Area | Problems Solved |
| -------------------------------- | ---------------------------- | --------------- |
| [LeetCode](#-leetcode) | Data Structures & Algorithms | 161 |
| [LeetCode](#-leetcode) | Data Structures & Algorithms | 162 |
| [GreatFrontEnd](#-greatfrontend) | Frontend Engineering | 5 |

## 🎯 Platforms
Expand All @@ -18,7 +18,7 @@ A comprehensive collection of coding challenges from multiple platforms for lear
**Progress**:

- Easy: 80 problems
- Medium: 69 problems
- Medium: 70 problems
- Hard: 12 problems

**Quick Links**:
Expand Down Expand Up @@ -180,11 +180,12 @@ A comprehensive collection of coding challenges from multiple platforms for lear
</details>

<details>
<summary>🟡 Medium Problems (69 solved)</summary>
<summary>🟡 Medium Problems (70 solved)</summary>

### Stack & Design

- [0155 - Min Stack](./leetcode/medium/0155-min-stack) ![Medium](https://img.shields.io/badge/Medium-orange)
- [0208 - Implement Trie (Prefix Tree)](./leetcode/medium/0208-implement-trie-prefix-tree) ![Medium](https://img.shields.io/badge/Medium-orange)

### Array & Two Pointers

Expand Down
36 changes: 36 additions & 0 deletions leetcode/medium/0208-implement-trie-prefix-tree/README.md
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) ![Medium](https://img.shields.io/badge/Medium-orange)

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`.
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
Copy link

Copilot AI Dec 15, 2025

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".

Suggested change
// If char not found in current.children then the word does not exists
// If char not found in current.children then the word does not exist

Copilot uses AI. Check for mistakes.
// 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
Copy link

Copilot AI Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error: "exits" should be "exists" (the verb form meaning "is present").

Suggested change
// Full prefix path exits
// Full prefix path exists

Copilot uses AI. Check for mistakes.
return true;
}
}

/**
* Your Trie object will be instantiated and called as such:
* var obj = new Trie()
* obj.insert(word)
* var param_2 = obj.search(word)
* var param_3 = obj.startsWith(prefix)
*/
Loading