Skip to content

Commit 716304c

Browse files
committed
add solution: word-break
1 parent 46cbe9f commit 716304c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
๋ฌธ์ œ: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์‚ฌ์ „์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.
3+
ํ’€์ด: ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ(DP)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ๊ฐ ์œ„์น˜๊นŒ์ง€ ์‚ฌ์ „์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n*m) (n์€ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด, m์€ ๋‹จ์–ด ์‚ฌ์ „์˜ ๋‹จ์–ด ์ˆ˜)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
'''
7+
8+
9+
class Solution:
10+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
11+
dp = [False for i in range(len(s))]
12+
for i in wordDict:
13+
if i == s[:len(i)]:
14+
dp[len(i)-1] = True
15+
16+
for i in range(1, len(s)):
17+
if dp[i-1] == True:
18+
19+
for j in wordDict:
20+
if i+len(j) <= len(s) and j == s[i:i+len(j)]:
21+
dp[i+len(j)-1] = True
22+
return dp[len(s)-1]
23+

0 commit comments

Comments
ย (0)