Skip to content

Commit

Permalink
Decode Ways solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jungsiroo committed Dec 20, 2024
1 parent 84fe468 commit 6ac686f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions decode-ways/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def numDecodings(self, s: str) -> int:
"""
dp ์ด์šฉ : ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด๋†“๊ณ  ๊ทธ ๋’ค์— ๋ถ™์„ ์ˆ˜ ์žˆ๋Š”๊ฐ€?
ex) 1234
(1,2) -> (1,2,3)
(12) -> (12,3)
(1) -> (1,23)
Tc : O(n) / Sc : O(n)
"""
if s[0] == '0': return 0

n = len(s)
dp = [0]*(n+1)
dp[0] = dp[1] = 1

for i in range(2, n+1):
one = int(s[i-1])
two = int(s[i-2:i])

if 1<=one<=9: dp[i] += dp[i-1]
if 10<=two<=26 : dp[i] += dp[i-2]

return dp[n]

0 comments on commit 6ac686f

Please sign in to comment.