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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ Each problem follows this structure:

## Solutions

📊 **Statistics**: 87 problems solved (migrated to new structure)
📊 **Statistics**: 88 problems solved (migrated to new structure)

| Difficulty | Count |
|------------|-------|
| Easy | 61 |
| Medium | 25 |
| Medium | 26 |
| Hard | 1 |

### Browse by Difficulty
Expand Down
51 changes: 0 additions & 51 deletions Trie/208. Implement Trie (Prefix Tree).py

This file was deleted.

3 changes: 2 additions & 1 deletion docs/MEDIUM.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Medium Problems

Total: 25 problems solved
Total: 26 problems solved

## Solutions

Expand All @@ -22,6 +22,7 @@ Total: 25 problems solved
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [solution.py](../problems/medium/house_robber_198/solution.py) | [test_solution.py](../problems/medium/house_robber_198/test_solution.py) |
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [solution.py](../problems/medium/number_of_islands_200/solution.py) | [test_solution.py](../problems/medium/number_of_islands_200/test_solution.py) |
| 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | [solution.py](../problems/medium/course_schedule_207/solution.py) | [test_solution.py](../problems/medium/course_schedule_207/test_solution.py) |
| 208 | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | [solution.py](../problems/medium/implement_trie_prefix_tree_208/solution.py) | [test_solution.py](../problems/medium/implement_trie_prefix_tree_208/test_solution.py) |
| 230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [solution.py](../problems/medium/kth_smallest_element_in_a_bst_230/solution.py) | [test_solution.py](../problems/medium/kth_smallest_element_in_a_bst_230/test_solution.py) |
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [solution.py](../problems/medium/lowest_common_ancestor_of_a_binary_search_tree_235/solution.py) | [test_solution.py](../problems/medium/lowest_common_ancestor_of_a_binary_search_tree_235/test_solution.py) |
| 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [solution.py](../problems/medium/lowest_common_ancestor_of_a_binary_tree_236/solution.py) | [test_solution.py](../problems/medium/lowest_common_ancestor_of_a_binary_tree_236/test_solution.py) |
Expand Down
Empty file.
55 changes: 55 additions & 0 deletions problems/medium/implement_trie_prefix_tree_208/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Tags: Hash Table, String, Design, Trie


class Trie:

def __init__(self):
self.links: dict[str, Trie] = {}
self.is_end = False

def insert(self, word: str) -> None:
"""
Time complexity : O(m), where m is the key(word) length.
In each iteration of the algorithm,
we either examine or create a node in the trie till we reach the end of the key.
This takes only m operations.

Space complexity : O(m).
In the worst case newly inserted key doesn't share
a prefix with the keys already inserted in the trie.
We have to add m new nodes, which takes us O(m) space.
"""
curr = self
for c in word:
if c not in curr.links:
curr.links[c] = Trie()
curr = curr.links[c]
curr.is_end = True

def search(self, word: str) -> bool:
"""
Time complexity : O(m)
In each step of the algorithm we search for the next key character.
In the worst case the algorithm performs m operations.

Space complexity : O(1)
"""
curr = self
for c in word:
if c not in curr.links:
return False
curr = curr.links[c]
return curr.is_end

def starts_with(self, prefix: str) -> bool:
"""
Time complexity : O(m)

Space complexity : O(1)
"""
curr = self
for c in prefix:
if c not in curr.links:
return False
curr = curr.links[c]
return True
38 changes: 38 additions & 0 deletions problems/medium/implement_trie_prefix_tree_208/test_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from .solution import Trie


class TestSolution:

def call(self, methods, params, expected):
results = []
obj = None
for method, param in zip(methods, params):
if method == "Trie":
obj = Trie()
results.append(None)
else:
result = getattr(obj, method)(*param)
results.append(result)
assert results == expected

def test_example_1(self):
"""
Example 1:
Input: ["Trie", "insert", "search", "search", "starts_with", "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.starts_with("app"); // return True
trie.insert("app");
trie.search("app"); // return True
"""
self.call(
["Trie", "insert", "search", "search", "starts_with", "insert", "search"],
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]],
[None, None, True, False, True, None, True],
)
Loading