-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 공간복잡도 O(n): dictionary 멤버로 set을 사용 | ||
# 시간복잡도 O(n*p): 삽입연산은 O(1)을 사용 | ||
import re | ||
|
||
class WordDictionary: | ||
|
||
def __init__(self): | ||
self.dictionary = set() | ||
|
||
def addWord(self, word: str) -> None: | ||
self.dictionary.add(word) | ||
|
||
def search(self, word: str) -> bool: | ||
if '.' in word: | ||
pattern = re.compile(word) | ||
# O(n) times | ||
for item in self.dictionary: | ||
# O(p) times : 패턴의 길이(p) | ||
if pattern.fullmatch(item): | ||
return True | ||
return False | ||
else: | ||
return word in self.dictionary |