Skip to content

Commit 7fb7220

Browse files
author
sejineer
committed
decode-ways solution
1 parent 84c1a0e commit 7fb7220

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

decode-ways/sejineer.py

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

0 commit comments

Comments
 (0)