Skip to content

Commit 2dbaefc

Browse files
authored
[ PS ] : Decode Ways
1 parent 1685345 commit 2dbaefc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

β€Ždecode-ways/uraflower.jsβ€Ž

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* μ£Όμ–΄μ§„ λ¬Έμžμ—΄μ„ λ³΅ν˜Έν™”ν•˜λŠ” 경우의 수λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
const numDecodings = function(s) {
7+
const dp = {};
8+
9+
function decode(idx) {
10+
if (s[idx] === '0') {
11+
return 0;
12+
}
13+
14+
if (idx === s.length) {
15+
return 1;
16+
}
17+
18+
if (dp[idx]) {
19+
return dp[idx];
20+
}
21+
22+
let result = 0;
23+
result += decode(idx + 1); // ν˜„μž¬ 문자만 μ“°λŠ” 경우: λ‹€μŒ λ¬ΈμžλΆ€ν„° 탐색
24+
if (s[idx + 1] && Number(s[idx] + s[idx+1]) <= 26) {
25+
result += decode(idx + 2); // ν˜„μž¬ λ¬Έμžμ™€ λ‹€μŒ 문자 λΆ™μ—¬μ„œ μ“°λŠ” 경우: λ‹€λ‹€μŒ λ¬ΈμžλΆ€ν„° 탐색
26+
}
27+
28+
dp[idx] = result;
29+
return result;
30+
}
31+
32+
return decode(0);
33+
};
34+
35+
// μ‹œκ°„λ³΅μž‘λ„: O(n) (λ©”λͺ¨μ΄μ œμ΄μ…˜ μ•ˆν•˜λ©΄ λ§€ μΈλ±μŠ€λ§ˆλ‹€ μ΅œλŒ€ 2개의 ν•˜μœ„ 호좜이 λ°œμƒν•˜μ—¬ O(2^n))
36+
// κ³΅κ°„λ³΅μž‘λ„: O(n)

0 commit comments

Comments
Β (0)