File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change
1
+ # ๋์ฝ๋ ๊ฐ๋ฅ 1 ~ 26
2
+ # ์ซ์์ ์ฒซ ๋ฒ์งธ ์๋ฆฌ๊ฐ 0์ด๋ผ๋ฉด decode x
3
+ # O(n) time, O(n) space
4
+
5
+ class Solution :
6
+ def numDecodings (self , s : str ) -> int :
7
+ memo = {len (s ): 1 } # ๋ฌธ์์ด ๋์ ๋๋ฌํ์ ๋๋ ๊ฒฝ์ฐ์ ์ 1
8
+
9
+ def dfs (start ):
10
+ if start in memo : # ์ด๋ฏธ ๊ณ์ฐํ ์์น ์ฌ๊ณ์ฐ x
11
+ return memo [start ]
12
+ if s [start ] == "0" :
13
+ memo [start ] = 0
14
+ elif start + 1 < len (s ) and int (s [start :start + 2 ]) < 27 : # ๋ ์๋ฆฌ๋ก ํด์ ๊ฐ๋ฅํ ๋
15
+ memo [start ] = dfs (start + 1 ) + dfs (start + 2 ) # ์ฒซ ํ ์๋ฆฌ๋ง decode ๊ฒฝ์ฐ + ๋ ์๋ฆฌ ํ๊บผ๋ฒ์ decode ๊ฒฝ์ฐ
16
+ else :
17
+ memo [start ] = dfs (start + 1 ) # ๋ ์๋ฆฌ๋ก decode ๋ถ๊ฐ๋ฅํ ๋ -> ํ ์๋ฆฌ๋ง decode
18
+ return memo [start ]
19
+ return dfs (0 )
You canโt perform that action at this time.
0 commit comments