Skip to content

Commit 8286332

Browse files
committed
decode-ways solution
1 parent 8be06a0 commit 8286332

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* μΈμ½”λ“œ 된 λ©”μ„Έμ§€λ₯Ό λ””μ½”λ“œ(decode)ν•˜λ €λ©΄, λ§΅ν•‘μ˜ κ·œμΉ™μ„ λ°˜λŒ€λ‘œ μ μš©ν•˜μ—¬ 숫자λ₯Ό λ‹€μ‹œ μ•ŒνŒŒλ²³ λŒ€λ¬Έμžλ‘œ λŒλ €λ†”μ•Ό ν•œλ‹€.
3+
* 예λ₯Ό λ“€μ–΄, 숫자 11106은 1 1 10 6으둜 λΆ„ν• ν•˜λ©΄ AAJF둜 λ””μ½”λ“œν•  μˆ˜λ„ 있고, 11 10 6으둜 λΆ„ν• ν•˜λ©΄ KJFλ‘œλ„ λ””μ½”λ“œν• 
4+
수 μžˆλ‹€.
5+
* λ¬Έμžμ—΄ νƒ€μž…μ˜ μˆ«μžκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ μ•ŒνŒŒλ²³ λ¬Έμžμ—΄λ‘œ λ””μ½”λ“œν•  수 μžˆλŠ” λ°©λ²•μ˜ 개수λ₯Ό κ΅¬ν•˜λΌ.
6+
* @param {string} s
7+
* @return {number}
8+
*/
9+
var numDecodings = function(s) {
10+
const memo = new Map();
11+
memo.set(s.length, 1);
12+
13+
function dfs(start) {
14+
if (memo.has(start)) {
15+
return memo.get(start);
16+
}
17+
18+
// 0으둜 μ‹œμž‘ν•˜λ©΄ 해석 λΆˆκ°€
19+
if (s[start] === "0") {
20+
memo.set(start, 0);
21+
return 0;
22+
}
23+
24+
let count = dfs(start + 1); // ν•œ κΈ€μž 해석
25+
26+
// 두 κΈ€μž 해석 κ°€λŠ₯ν•œ 경우
27+
if (start + 1 < s.length && parseInt(s.substring(start, start + 2)) <= 26) {
28+
count += dfs(start + 2);
29+
}
30+
31+
memo.set(start, count);
32+
return count;
33+
}
34+
35+
return dfs(0);
36+
};

0 commit comments

Comments
Β (0)