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 6544db9 commit fd2cd7aCopy full SHA for fd2cd7a
โword-break/jungsiroo.pyโ
@@ -0,0 +1,27 @@
1
+class Solution:
2
+ def wordBreak(self, s: str, wordDict: List[str]) -> bool:
3
+ # DP๋ฅผ ํ์ฉํ ๋ฌธ์
4
+
5
+ """
6
+ KMP๋ Rabin Karp๋ฅผ ์ด์ฉํ์ฌ ํธ๋ ๋ฌธ์ ์ธ ์ค ์์์ผ๋ ์ ํ ์๋ ๋ฌธ์
7
+ neetcode์ ๋์์ ๋ฐ์์
8
9
+ ๊ณ์ ์ํ๋ฅผ ํ๋ฉด์ if dp[j]๋ฅผ ํตํด ๊ธธ์ด๋งํผ ๋์ด์ฃผ๊ณ word_set์ ์๋์ง๋ฅผ ํ์ธํจ
10
+ Time Complexity : O(n^2)
11
+ Space Complexity : O(n)
12
13
14
+ word_set = set(wordDict)
15
+ n = len(s)
16
17
+ dp = [False] * (n + 1)
18
+ dp[0] = True
19
20
+ for i in range(1, n + 1):
21
+ for j in range(i):
22
+ if dp[j] and s[j:i] in word_set:
23
+ dp[i] = True
24
+ break
25
26
+ return dp[n]
27
0 commit comments