Skip to content

Commit b56f2c5

Browse files
committed
[week 5] solve 3
1 parent 9d26168 commit b56f2c5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Blind 75 - Word Break
3+
LeetCode Problem: https://leetcode.com/problems/word-break/
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(n^2)
5+
๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
6+
ํ’€์ด : ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ด์šฉํ•œ ํ’€์ด
7+
๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๋ฅผ n์ด๋ผ ํ•  ๋•Œ, dp[i]๋ฅผ s์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ i-1๋ฒˆ์งธ ๋ฌธ์ž๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด
8+
๋‹จ์–ด ์‚ฌ์ „์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์ด๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.
9+
"""
10+
from typing import List
11+
class Solution:
12+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
13+
dp = [False] * (len(s) + 1)
14+
dp[0] = True
15+
16+
word_set = set(wordDict)
17+
for i in range(1, len(s) + 1):
18+
for j in range(i):
19+
if dp[j] and s[j:i] in word_set: # 0~j-1๊นŒ์ง€ ๋ฌธ์ž์—ด์ด ๋‹จ์–ด์‚ฌ์ „์— ์žˆ๊ณ , j~i-1๊นŒ์ง€ ๋ฌธ์ž์—ด์ด ๋‹จ์–ด์‚ฌ์ „์— ์žˆ๋Š” ๊ฒฝ์šฐ
20+
dp[i] = True
21+
break
22+
23+
return dp[len(s)]

0 commit comments

Comments
ย (0)