-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathsangbeenmoon.py
More file actions
29 lines (22 loc) ยท 862 Bytes
/
sangbeenmoon.py
File metadata and controls
29 lines (22 loc) ยท 862 Bytes
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
# ์คํจํ ํ์ด.
# AC ๋ฅผ ๋ฐ๊ธฐ๋ ํ์ผ๋ test case ๊ฐ ์ข ๋ ์ด์ดํ๋ค๋ฉด TLE ์ ๊ฑธ๋ ธ์ ๊ฒ์.
# string ์ด ์๋ index ๋ก memoization ์ ํ๋ ๊ฑธ ๋ ์ฌ๋ ค๋ณด์.
class Solution:
answer = False
visited = {}
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
self.answer = False
self.visited = {}
self.go(0,s,wordDict)
return self.answer
def go(self, i:int, s: str, wordDict: List[str]):
if i >= (len(s)):
self.answer = True
return
for word in wordDict:
if i + len(word) > len(s):
continue
if word == s[i:i + len(word)]:
if not s[i + len(word) : ] in self.visited:
self.go(i + len(word), s, wordDict)
self.visited[s[i:]] = False