@@ -29,10 +29,19 @@ def wordBreak(self, s: str, wordDict: List[str]) -> bool:
2929
3030 n = len (s ) # ๋ฌธ์์ด s์ ๊ธธ์ด, ๋์ค์ ์ธ๋ฑ์ค ๋๊น์ง ๋๋ฌํ๋์ง ํ์ธํ๊ธฐ ์ํด ์ฌ์ฉํจ
3131
32+ # <<<--- ๋ฉ๋ชจ์ด์ ์ด์
์บ์ ์ด๊ธฐํ ---<<<
33+ # key: start_index, value: s[start_index:] ๋ถํ ๊ฐ๋ฅ ์ฌ๋ถ (True/False)
34+ memo = {}
35+
3236 # 2. ์ฌ๊ท ํจ์ ์ ์
3337 # canBreak(start_index): s[strat_index:] ๋ถ๋ถ์ ๋ถํ ํ ์ ์๋์ง ํ์ธ
3438 def canBreak (start_index : int ) -> bool :
3539
40+ # <<<--- ์บ์ ํ์ธ ---<<<
41+ # ์ด start_index์ ๋ํ ๊ฒฐ๊ณผ๊ฐ ์ด๋ฏธ memo์ ์์ผ๋ฉด ๋ฐ๋ก ๋ฐํ
42+ if start_index in memo :
43+ return memo [start_index ]
44+
3645 # ๋ฒ ์ด์ค ์ผ์ด์ค
3746 # ์์ ์ธ๋ฑ์ค(start_index)๊ฐ ๋ฌธ์์ด ๋์ ๋๋ฌํ๋ค๋ฉด ์ฑ๊ณต
3847 if start_index == n :
@@ -41,7 +50,6 @@ def canBreak(start_index: int) -> bool:
4150 # ํ์ฌ start_index๋ถํฐ ์์ํ๋ ๊ฐ๋ฅํ ๋ชจ๋ ๋จ์ด๋ฅผ ํธ๋ผ์ด๋ฅผ ์ด์ฉํด ์ฐพ๊ณ
4251 # ๊ฐ ๋จ์ด์ ๋ํด ๋๋จธ์ง ๋ถ๋ถ์ด ๋ถํ ๊ฐ๋ฅํ์ง ์ฌ๊ท์ ์ผ๋ก ํ์ธ
4352 currentNode = trie .root
44-
4553 for i in range (start_index , n ):
4654 char = s [i ]
4755
@@ -57,9 +65,13 @@ def canBreak(start_index: int) -> bool:
5765 # ๋๋จธ์ง ๋ถ๋ถ s[i+1:]์ ๋ํด์๋ ๋ถํ ๊ฐ๋ฅํ์ง ์ฌ๊ท ํธ์ถ
5866 if canBreak (i + 1 ):
5967 # ๋๋จธ์ง ๋ถ๋ถ ๋ถํ ์ฑ๊ณต => ์ ์ฒด ๋ถํ ๊ฐ๋ฅ
68+ # <<<--- ์ฑ๊ณต ๊ฒฐ๊ณผ ์บ์์ ์ ์ฅ ---<<<
69+ memo [start_index ] = True
6070 return True
6171 # start_index๋ถํฐ ์์ํ๋ ๋ชจ๋ ๊ฐ๋ฅํ ๋จ์ด ๋ถํ ์ ์๋ํ์ผ๋
6272 # ์ฑ๊ณต์ ์ธ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ง ๋ชปํ๋ค๋ฉด
73+ # <<<--- ์คํจ ๊ฒฐ๊ณผ ์บ์์ ์ ์ฅ ---<<<
74+ memo [start_index ] = False
6375 return False
6476
6577 # 3. ์ฌ๊ท ํจ์ ํธ์ถ ์์
0 commit comments