Skip to content

Commit 0970fcf

Browse files
committed
word break
1 parent 3b760c7 commit 0970fcf

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

β€Žword-break/ICE0208.pyβ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
6+
word_set = set(wordDict)
7+
8+
@cache
9+
def finding(start):
10+
# λ¬Έμžμ—΄ λκΉŒμ§€ λ‹¨μ–΄λ“€λ‘œ λ‚˜λˆ„λŠ” 데 μ„±κ³΅ν•œ 경우
11+
if start == len(s):
12+
return True
13+
14+
# startλΆ€ν„° μ‹œμž‘ν•˜λŠ” λͺ¨λ“  λΆ€λΆ„ λ¬Έμžμ—΄μ„ ν™•μΈν•œλ‹€.
15+
for end in range(start + 1, len(s) + 1):
16+
current_word = s[start:end]
17+
18+
# ν˜„μž¬ 단어가 사전에 있고,
19+
# λ‚˜λ¨Έμ§€ λ¬Έμžμ—΄λ„ λ‚˜λˆŒ 수 μžˆλ‹€λ©΄ λ°”λ‘œ μ’…λ£Œν•œλ‹€.
20+
if current_word in word_set and finding(end):
21+
return True
22+
23+
# μ–΄λ–€ λ°©μ‹μœΌλ‘œλ„ λ‚˜λˆŒ 수 μ—†λŠ” 경우
24+
return False
25+
26+
return finding(0)

0 commit comments

Comments
Β (0)