1
1
"""
2
2
[๋ฌธ์ ํ์ด]
3
3
# Inputs
4
-
4
+ ์ซ์๋ก ๊ตฌ์ฑ๋ ๋ฌธ์์ด s
5
5
# Outputs
6
-
6
+ decode ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์
7
7
# Constraints
8
-
8
+ 1 <= s.length <= 100
9
+ s contains only digits and may contain leading zero(s).
9
10
# Ideas
11
+ ๋ชจ๋ ๊ฒฝ์ฐ์ ์ -> 1. ์ฐ์ ์ํ
12
+
13
+ 11106 -> 1 1 10 6
14
+ ๊ฐ๋ฅํ ์ซ์๋ค : 1~26
15
+ 11 10 6
16
+
17
+ ์ง์ง ์ํ๋ง ๊ฐ๋ฅํ์ง ์๋?
18
+ 1 1 1 0 6 (x)
19
+ 1 1 1 06 (x)
20
+ 1 1 10 6 (o)
21
+ 1 1 106 (x)
22
+ 11 1 0 6 (x)
23
+ 10 1 06 (x)
24
+
25
+ ์ฌ๊ท๋ก ๊ตฌํํ๋ฉด ๋ ๋ฏ
26
+
27
+ n = len(s)
28
+ ans = 0
29
+
30
+ def decode(start_idx):
31
+ global ans
32
+ if start_idx >= n :
33
+ return
34
+ for i in range(1, 2):
35
+ num = int(s[start_idx:start_idx + i]
36
+ if i == 1:
37
+ if num != 0:
38
+ ans += 1
39
+ decode(start_idx + 1)
40
+ elif i == 2 :
41
+ if s[start_idx:start_idx+i][0] != 0 and 10 <= num <= 26:
42
+ ans += 1
43
+ decode(start_idx + 1)
44
+
45
+
46
+ 2. ์ํ ์ฝ๋ ์๊ฐ ์ด๊ณผ
47
+ ๊ทผ๋ฐ ์ด๊ฑฐ climbing stairs๋ ์ ์ฌํ ๋ฌธ์ ์๋๊ฐ?
48
+ 1๊ณผ 2๋ก ์ค๋ฅผ ์ ์๋ ๊ฒฝ์ฐ์ ์ -> ํ์ง๋ง, ์ซ์ ํํ ์กฐ๊ฑด์ด ๊ฑธ๋ ค์์ด์ ์..
49
+
50
+ 2
51
+ 2
52
+ 11
53
+ 10240
54
+
55
+ 3
56
+
57
+
10
58
11
59
[ํ๊ณ ]
60
+ ์ฒซ ์ ์ถ ์ฝ๋๋ ์๊ฐ์ด๊ณผ ๋ฐ์
61
+ ๋ฉ๋ชจ์ด์ ์ด์
์ผ๋ก ์ฝ๋ ๊ฐ์ ๊ฐ๋ฅ
62
+ """
63
+
64
+ ## ์ฒซ ์ ์ถ ์ฝ๋
65
+ class Solution :
66
+ def numDecodings (self , s : str ) -> int :
67
+ n = len (s )
68
+
69
+ # ans๋ฅผ ๋ณ์๋ก ๋๊ณ ๋ฐํ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ
70
+ def decode (start_idx ):
71
+ if start_idx >= n :
72
+ return 1 # ๋์ ๋๋ฌํ๋ฉด 1์ ๋ฐํ (๋๊น์ง ๋๋ฌํ๋ฉด ์ ํจํ ๋์ฝ๋ฉ ๋ฐฉ๋ฒ์)
73
+
74
+ ans = 0
75
+ for i in range (1 , 3 ): # 1์๋ฆฌ ๋๋ 2์๋ฆฌ ์ซ์๋ฅผ ํ์ธ
76
+ if start_idx + i <= n : # ์ธ๋ฑ์ค ๋ฒ์ ํ์ธ
77
+ num = int (s [start_idx :start_idx + i ])
78
+ if i == 1 :
79
+ if num != 0 : # 1์๋ฆฌ ์ซ์๊ฐ 0์ด ์๋๋ฉด ์งํ
80
+ ans += decode (start_idx + 1 )
81
+ else :
82
+ if 10 <= num <= 26 : # 2์๋ฆฌ ์ซ์๊ฐ 10์์ 26 ์ฌ์ด์ผ ๋ ์งํ
83
+ ans += decode (start_idx + 2 )
84
+
85
+ return ans
86
+
87
+ return decode (0 ) # 0๋ถํฐ ์์
88
+
89
+ # ๋๋ฒ์งธ ์ ์ถ ์ฝ๋
90
+
91
+ class Solution :
92
+ def numDecodings (self , s : str ) -> int :
93
+ memo = {len (s ): 1 }
94
+ n = len (s )
95
+
96
+ # ans๋ฅผ ๋ณ์๋ก ๋๊ณ ๋ฐํ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ
97
+ def dfs (start ):
98
+ if start in memo :
99
+ return memo [start ]
100
+
101
+ if s [start ] == "0" :
102
+ memo [start ] = 0
103
+ elif start + 1 < n and int (s [start :start + 2 ]) < 27 :
104
+ memo [start ] = dfs (start + 1 ) + dfs (start + 2 )
105
+ else :
106
+ memo [start ] = dfs (start + 1 )
107
+
108
+ return memo [start ]
109
+
110
+ return dfs (0 ) # 0๋ถํฐ ์์
111
+
112
+ # ํด์ค์ Bottom-up ๋ฐฉ์์ด ์ดํด๊ฐ ์๊ฐ ๋๋ฒ๊น
์๋
113
+ class Solution :
114
+ def numDecodings (s ):
115
+ dp = [0 ] * len (s ) + [1 ]
116
+ for start in reversed (range (len (s ))):
117
+ if s [start ] == "0" :
118
+ dp [start ] = 0
119
+ elif start + 1 < len (s ) and int (s [start : start + 2 ]) < 27 :
120
+ dp [start ] = dp [start + 1 ] + dp [start + 2 ]
121
+ else :
122
+ dp [start ] = dp [start + 1 ]
123
+ return dp [0 ]
12
124
13
- """
125
+ numDecodings ( "2266" )
0 commit comments