We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1fe3839 commit 151579cCopy full SHA for 151579c
1 file changed
โword-break/sangbeenmoon.pyโ
@@ -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