@@ -29,10 +29,19 @@ def wordBreak(self, s: str, wordDict: List[str]) -> bool:
29
29
30
30
n = len (s ) # ๋ฌธ์์ด s์ ๊ธธ์ด, ๋์ค์ ์ธ๋ฑ์ค ๋๊น์ง ๋๋ฌํ๋์ง ํ์ธํ๊ธฐ ์ํด ์ฌ์ฉํจ
31
31
32
+ # <<<--- ๋ฉ๋ชจ์ด์ ์ด์
์บ์ ์ด๊ธฐํ ---<<<
33
+ # key: start_index, value: s[start_index:] ๋ถํ ๊ฐ๋ฅ ์ฌ๋ถ (True/False)
34
+ memo = {}
35
+
32
36
# 2. ์ฌ๊ท ํจ์ ์ ์
33
37
# canBreak(start_index): s[strat_index:] ๋ถ๋ถ์ ๋ถํ ํ ์ ์๋์ง ํ์ธ
34
38
def canBreak (start_index : int ) -> bool :
35
39
40
+ # <<<--- ์บ์ ํ์ธ ---<<<
41
+ # ์ด start_index์ ๋ํ ๊ฒฐ๊ณผ๊ฐ ์ด๋ฏธ memo์ ์์ผ๋ฉด ๋ฐ๋ก ๋ฐํ
42
+ if start_index in memo :
43
+ return memo [start_index ]
44
+
36
45
# ๋ฒ ์ด์ค ์ผ์ด์ค
37
46
# ์์ ์ธ๋ฑ์ค(start_index)๊ฐ ๋ฌธ์์ด ๋์ ๋๋ฌํ๋ค๋ฉด ์ฑ๊ณต
38
47
if start_index == n :
@@ -41,7 +50,6 @@ def canBreak(start_index: int) -> bool:
41
50
# ํ์ฌ start_index๋ถํฐ ์์ํ๋ ๊ฐ๋ฅํ ๋ชจ๋ ๋จ์ด๋ฅผ ํธ๋ผ์ด๋ฅผ ์ด์ฉํด ์ฐพ๊ณ
42
51
# ๊ฐ ๋จ์ด์ ๋ํด ๋๋จธ์ง ๋ถ๋ถ์ด ๋ถํ ๊ฐ๋ฅํ์ง ์ฌ๊ท์ ์ผ๋ก ํ์ธ
43
52
currentNode = trie .root
44
-
45
53
for i in range (start_index , n ):
46
54
char = s [i ]
47
55
@@ -57,9 +65,13 @@ def canBreak(start_index: int) -> bool:
57
65
# ๋๋จธ์ง ๋ถ๋ถ s[i+1:]์ ๋ํด์๋ ๋ถํ ๊ฐ๋ฅํ์ง ์ฌ๊ท ํธ์ถ
58
66
if canBreak (i + 1 ):
59
67
# ๋๋จธ์ง ๋ถ๋ถ ๋ถํ ์ฑ๊ณต => ์ ์ฒด ๋ถํ ๊ฐ๋ฅ
68
+ # <<<--- ์ฑ๊ณต ๊ฒฐ๊ณผ ์บ์์ ์ ์ฅ ---<<<
69
+ memo [start_index ] = True
60
70
return True
61
71
# start_index๋ถํฐ ์์ํ๋ ๋ชจ๋ ๊ฐ๋ฅํ ๋จ์ด ๋ถํ ์ ์๋ํ์ผ๋
62
72
# ์ฑ๊ณต์ ์ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ง ๋ชปํ๋ค๋ฉด
73
+ # <<<--- ์คํจ ๊ฒฐ๊ณผ ์บ์์ ์ ์ฅ ---<<<
74
+ memo [start_index ] = False
63
75
return False
64
76
65
77
# 3. ์ฌ๊ท ํจ์ ํธ์ถ ์์
0 commit comments