File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ # ์คํจํ ํ์ด.
2+ # AC ๋ฅผ ๋ฐ๊ธฐ๋ ํ์ผ๋ test case ๊ฐ ์ข ๋ ์ด์ดํ๋ค๋ฉด TLE ์ ๊ฑธ๋ ธ์ ๊ฒ์.
3+ # string ์ด ์๋ index ๋ก memoization ์ ํ๋ ๊ฑธ ๋ ์ฌ๋ ค๋ณด์.
4+
5+ class Solution :
6+ answer = False
7+ visited = {}
8+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
9+
10+ self .answer = False
11+ self .visited = {}
12+ self .go (0 ,s ,wordDict )
13+
14+ return self .answer
15+
16+ def go (self , i :int , s : str , wordDict : List [str ]):
17+ if i >= (len (s )):
18+ self .answer = True
19+ return
20+
21+ for word in wordDict :
22+ if i + len (word ) > len (s ):
23+ continue
24+
25+ if word == s [i :i + len (word )]:
26+ if not s [i + len (word ) : ] in self .visited :
27+ self .go (i + len (word ), s , wordDict )
28+
29+ self .visited [s [i :]] = False
You canโt perform that action at this time.
0 commit comments