Skip to content

Commit 31235d2

Browse files
committed
feat: add decode-ways solution
1 parent ef67ff9 commit 31235d2

File tree

1 file changed

+116
-4
lines changed

1 file changed

+116
-4
lines changed

โ€Ždecode-ways/shinsj4653.pyโ€Ž

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,125 @@
11
"""
22
[๋ฌธ์ œํ’€์ด]
33
# Inputs
4-
4+
์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด s
55
# Outputs
6-
6+
decode ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜
77
# Constraints
8-
8+
1 <= s.length <= 100
9+
s contains only digits and may contain leading zero(s).
910
# 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+
1058
1159
[ํšŒ๊ณ ]
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]
12124

13-
"""
125+
numDecodings("2266")

0 commit comments

Comments
ย (0)