-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path211_AddandSearchWord.py
executable file
·99 lines (87 loc) · 2.83 KB
/
211_AddandSearchWord.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
import collections
class WordDictionary(object):
# One faster, easy understand way
# Refer to:
# https://leetcode.com/discuss/69963/python-168ms-beat-100%25-solution
def __init__(self):
self.words_dict = collections.defaultdict(list)
def addWord(self, word):
if word:
self.words_dict[len(word)].append(word)
def search(self, word):
"""
Returns if the word is in the data structure. A word could
contain the dot character '.' to represent any one letter.
"""
if not word:
return False
for w in self.words_dict[len(word)]:
is_match = True
for i, ch in enumerate(word):
if ch != "." and ch != w[i]:
is_match = False
break
if is_match:
return True
return False
class TrieNode():
# Refer to: 208. Implement Trie
def __init__(self):
self.is_word = False
self.childrens = {}
class WordDictionary_Trie(object):
def __init__(self):
self.root = TrieNode()
def addWord(self, word):
"""
Adds a word into the data structure.
"""
cur_node = self.root
for ch in word:
if ch not in cur_node.childrens:
cur_node.childrens[ch] = TrieNode()
cur_node = cur_node.childrens[ch]
cur_node.is_word = True
def search(self, word):
"""
Returns if the word is in the data structure. A word could
contain the dot character '.' to represent any one letter.
"""
return self._dfs_searh(word, self.root)
# Depth First Search the trie tree.
def _dfs_searh(self, word, cur_node):
if not word and cur_node.is_word:
return True
word_len = len(word)
for i in range(word_len):
ch = word[i]
if ch == ".":
for child_ch in cur_node.childrens:
if self._dfs_searh(word[i+1:],
cur_node.childrens[child_ch]):
return True
return False
else:
if ch not in cur_node.childrens:
return False
else:
cur_node = cur_node.childrens[ch]
if cur_node.is_word:
return True
else:
return False
"""
if __name__ == '__main__':
wordDictionary = WordDictionary()
wordDictionary.addWord("bad")
wordDictionary.addWord("dad")
wordDictionary.addWord("mad")
print wordDictionary.search("xad")
print wordDictionary.search(".a")
print wordDictionary.search(".ad")
print wordDictionary.search("b.")
print wordDictionary.search(".")
"""