-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_implement_trie.py
149 lines (121 loc) · 4.63 KB
/
14_implement_trie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
##########################################################
# Author: Raghav Sikaria
# LinkedIn: https://www.linkedin.com/in/raghavsikaria/
# Github: https://github.com/raghavsikaria
# Last Update: 14-5-2020
# Project: LeetCode May 31 Day 2020 Challenge - Day 14
##########################################################
# QUESTION
# This question is from the abovementioned challenge and can be found here on LeetCode: https://leetcode.com/explore/featured/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3329/
############################################################################
# Implement a trie with insert, search, and startsWith methods.
# Example:
# Trie trie = new Trie();
# trie.insert("apple");
# trie.search("apple"); // returns true
# trie.search("app"); // returns false
# trie.startsWith("app"); // returns true
# trie.insert("app");
# trie.search("app"); // returns true
# Note:
# You may assume that all inputs are consist of lowercase letters a-z.
# All inputs are guaranteed to be non-empty strings.
############################################################################
# ACCEPTED SOLUTION #1
# Personal Comments: Had always coded Tries purely in C++-14
# But coding it out in Python was wuite liberating!
# Was quite disappointed seeing everyone use Python Dictionaries for NEW NODE
# and LeetCode not having good cases to test that out.
# [NEW-LEARNING][SOURCE]: https://www.geeksforgeeks.org/trie-insert-and-search/
class TrieNode:
def __init__(self):
self.children = [None]*26
self.is_end_of_world = False
class Trie:
get_index = lambda x: ord(x) - ord('a')
get_node = lambda: TrieNode()
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = Trie.get_node()
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
ite = self.root
for alphabet in [Trie.get_index(character) for character in word]:
if not ite.children[alphabet]: ite.children[alphabet] = Trie.get_node()
ite = ite.children[alphabet]
ite.is_end_of_world = True
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
ite = self.root
for alphabet in [Trie.get_index(character) for character in word]:
if not ite.children[alphabet]: return False
ite = ite.children[alphabet]
return ite != None and ite.is_end_of_world
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
ite = self.root
for alphabet in [Trie.get_index(character) for character in prefix]:
if not ite.children[alphabet]: return False
ite = ite.children[alphabet]
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
# ACCEPTED SOLUTION #2
# Personal Comments:
# Using Python Dictionaries for CHILDREN ARRAY for NEW NODE
# Since alphabets are only 26, small trade off for LOOKUP Time
# will make code more readable
class TrieNode:
def __init__(self):
self.children = {}
self.is_end_of_world = False
class Trie:
get_node = lambda: TrieNode()
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = Trie.get_node()
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
ite = self.root
for alphabet in word:
if alphabet not in ite.children: ite.children[alphabet] = Trie.get_node()
ite = ite.children[alphabet]
ite.is_end_of_world = True
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
ite = self.root
for alphabet in word:
if alphabet not in ite.children: return False
ite = ite.children[alphabet]
return ite != None and ite.is_end_of_world
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
ite = self.root
for alphabet in prefix:
if alphabet not in ite.children: return False
ite = ite.children[alphabet]
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)