Skip to content

Commit d3c9643

Browse files
author
jinbeom
committed
Decode Ways Solution
1 parent 5420d07 commit d3c9643

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

decode-ways/kayden.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 시간복잡도: O(N)
2+
# 공간복잡도: O(N)
3+
class Solution:
4+
def numDecodings(self, s: str) -> int:
5+
if s[0] == "0":
6+
return 0
7+
8+
n = len(s)
9+
dp = [0] * (n + 1)
10+
dp[0] = 1
11+
dp[1] = 1
12+
13+
for i in range(2, n + 1):
14+
15+
if int(s[i - 1]) != 0:
16+
dp[i] += dp[i - 1]
17+
18+
if 10 <= int(s[i - 2:i]) <= 26:
19+
dp[i] += dp[i - 2]
20+
21+
return dp[n]

0 commit comments

Comments
 (0)