Skip to content

Commit

Permalink
word-break solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jungsiroo committed Jan 5, 2025
1 parent 6544db9 commit fd2cd7a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions word-break/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
# DP๋ฅผ ํ™œ์šฉํ•œ ๋ฌธ์ œ

"""
KMP๋‚˜ Rabin Karp๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ์ธ ์ค„ ์•Œ์•˜์œผ๋‚˜ ์ „ํ˜€ ์•„๋‹Œ ๋ฌธ์ œ
neetcode์˜ ๋„์›€์„ ๋ฐ›์•˜์Œ
๊ณ„์† ์ˆœํšŒ๋ฅผ ํ•˜๋ฉด์„œ if dp[j]๋ฅผ ํ†ตํ•ด ๊ธธ์ด๋งŒํผ ๋Š์–ด์ฃผ๊ณ  word_set์— ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•จ
Time Complexity : O(n^2)
Space Complexity : O(n)
"""

word_set = set(wordDict)
n = len(s)

dp = [False] * (n + 1)
dp[0] = True

for i in range(1, n + 1):
for j in range(i):
if dp[j] and s[j:i] in word_set:
dp[i] = True
break

return dp[n]

0 comments on commit fd2cd7a

Please sign in to comment.