Skip to content

Commit

Permalink
feat: week 6 문제풀이 (257)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinah92 committed Jan 14, 2025
1 parent 174a7e1 commit 33aac17
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions design-add-and-search-words-data-structure/jinah92.py
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

0 comments on commit 33aac17

Please sign in to comment.