Skip to content

Commit 4f93e8c

Browse files
committed
leetcode.com 139. Word Break
문제 링크: https://leetcode.com/problems/word-break/description/
1 parent f75a587 commit 4f93e8c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
6+
dp: List[bool] = [False for _ in range(len(s))]
7+
l = len(s)
8+
bgns: List[int] = [0]
9+
10+
while len(bgns) > 0:
11+
bgn = bgns.pop(0)
12+
13+
for word in wordDict:
14+
if s[bgn:].startswith(word):
15+
bgn_new = bgn + len(word)
16+
if bgn_new == l:
17+
return True
18+
else:
19+
if not bgn_new in bgns and dp[bgn_new] == False:
20+
bgns.append(bgn_new)
21+
dp[bgn_new] = True
22+
23+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
class TestSolution(TestCase):
5+
def test1_word_break(self):
6+
sol = Solution()
7+
self.assertEqual(True, sol.wordBreak("leetcode", ["leet","code"]))
8+
9+
def test2_word_break(self):
10+
sol = Solution()
11+
self.assertEqual(True, sol.wordBreak("applepenapple", ["apple","pen"]))
12+
13+
def test3_word_break(self):
14+
sol = Solution()
15+
self.assertEqual(False, sol.wordBreak("catsandog", ["cats","dog","sand","and","cat"]))

0 commit comments

Comments
 (0)