Skip to content

Commit 4fae071

Browse files
authored
Add files via upload
1 parent a3a57e2 commit 4fae071

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Trie/Trie_add_Search_using_dict.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Trie:
2+
root = {}
3+
4+
def add(self, word):
5+
cur = self.root
6+
7+
for ch in word:
8+
if ch not in cur:
9+
cur[ch] = {}
10+
cur = cur[ch]
11+
cur['*'] = True
12+
13+
def search(self,word):
14+
cur = self.root
15+
16+
for ch in word:
17+
if ch not in cur:
18+
return False
19+
cur = cur[ch]
20+
if '*' in cur:
21+
return True
22+
else:
23+
return False
24+
dictionary = Trie()
25+
dictionary.add("pqr")
26+
dictionary.add("pqrst")
27+
print(dictionary.search("pqr"))
28+
print(dictionary.search("pq"))
29+
print(dictionary.search("p"))
30+
print(dictionary.search("pqrst"))
31+
dictionary.add("p")
32+
print(dictionary.search("p"))

0 commit comments

Comments
 (0)