Skip to content

Commit 6ac686f

Browse files
committed
Decode Ways solution
1 parent 84fe468 commit 6ac686f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

โ€Ždecode-ways/jungsiroo.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
"""
4+
dp ์ด์šฉ : ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด๋†“๊ณ  ๊ทธ ๋’ค์— ๋ถ™์„ ์ˆ˜ ์žˆ๋Š”๊ฐ€?
5+
ex) 1234
6+
(1,2) -> (1,2,3)
7+
(12) -> (12,3)
8+
(1) -> (1,23)
9+
10+
Tc : O(n) / Sc : O(n)
11+
"""
12+
if s[0] == '0': return 0
13+
14+
n = len(s)
15+
dp = [0]*(n+1)
16+
dp[0] = dp[1] = 1
17+
18+
for i in range(2, n+1):
19+
one = int(s[i-1])
20+
two = int(s[i-2:i])
21+
22+
if 1<=one<=9: dp[i] += dp[i-1]
23+
if 10<=two<=26 : dp[i] += dp[i-2]
24+
25+
return dp[n]
26+

0 commit comments

Comments
ย (0)