Skip to content

Commit 151579c

Browse files
author
sangbeenmoon
committed
tried word-break.
1 parent 1fe3839 commit 151579c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

โ€Žword-break/sangbeenmoon.pyโ€Ž

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

0 commit comments

Comments
ย (0)