Skip to content

Commit 0310c80

Browse files
committed
word-break solution
1 parent 70b935d commit 0310c80

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

โ€Žword-break/yyyyyyyyyKim.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
3+
4+
# DP
5+
dp = [False]*(len(s)+1)
6+
dp[0] = True # ๋นˆ๋ฌธ์ž์—ด์€ ๋‹จ์–ด์—†์ด๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ(True)
7+
8+
# i : s์˜ i๋ฒˆ์งธ ๋ฌธ์ž
9+
for i in range(1,len(s)+1):
10+
# j : ๋‹จ์–ด ์ž๋ฅด๋Š” ์‹œ์ž‘์œ„์น˜. s[:j]๊ฐ€ wordDict์— ์žˆ๊ณ , s[j:i]๊ฐ€ wordDict์— ์žˆ๋Š”์ง€ ํ™•์ธ.
11+
for j in range(i):
12+
if dp[j] and s[j:i] in wordDict:
13+
dp[i] = True # s[i]๊นŒ์ง€ ์ž๋ฅผ ์ˆ˜ ์žˆ์Œ(True)
14+
break # ์ž๋ฅผ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ ๋” ๋ณผ ํ•„์š” ์—†์Œ
15+
16+
return dp[len(s)]

0 commit comments

Comments
ย (0)