We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 174a7e1 commit 33aac17Copy full SHA for 33aac17
design-add-and-search-words-data-structure/jinah92.py
@@ -0,0 +1,23 @@
1
+# 공간복잡도 O(n): dictionary 멤버로 set을 사용
2
+# 시간복잡도 O(n*p): 삽입연산은 O(1)을 사용
3
+import re
4
+
5
+class WordDictionary:
6
7
+ def __init__(self):
8
+ self.dictionary = set()
9
10
+ def addWord(self, word: str) -> None:
11
+ self.dictionary.add(word)
12
13
+ def search(self, word: str) -> bool:
14
+ if '.' in word:
15
+ pattern = re.compile(word)
16
+ # O(n) times
17
+ for item in self.dictionary:
18
+ # O(p) times : 패턴의 길이(p)
19
+ if pattern.fullmatch(item):
20
+ return True
21
+ return False
22
+ else:
23
+ return word in self.dictionary
0 commit comments