Skip to content

Commit 33aac17

Browse files
committed
feat: week 6 문제풀이 (257)
1 parent 174a7e1 commit 33aac17

File tree

1 file changed

+23
-0
lines changed
  • design-add-and-search-words-data-structure

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)