Skip to content

Commit 5fc8bd8

Browse files
authored
Update 91-decode-ways.js
1 parent 4a54264 commit 5fc8bd8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

91-decode-ways.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const numDecodings = function(s) {
6+
if(s == null || s.length === 0) return 1
7+
if(s[0] === '0') return 0
8+
const set = new Set()
9+
const n = s.length
10+
for(let i = 1; i <= 26; i++) {
11+
set.add(`${i}`)
12+
}
13+
const dp = Array(n + 1).fill(0)
14+
dp[0] = dp[1] = 1
15+
for(let i = 2; i <= n; i++) {
16+
if(set.has(s[i - 2] + s[i - 1])) dp[i] += dp[i - 2]
17+
if(set.has(s[i - 1])) dp[i] += dp[i - 1]
18+
}
19+
return dp[n]
20+
};
21+
22+
23+
// another
24+
125
/**
226
* @param {string} s
327
* @return {number}

0 commit comments

Comments
 (0)