Skip to content

Commit 87b023c

Browse files
authored
solve decode ways
1 parent 9303e78 commit 87b023c

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

decode-ways/grapefruit13.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function numDecodings(s: string): number {
2+
if (s[0]==="0") return 0;
3+
4+
const dp = new Array(s.length).fill(0);
5+
dp[0] = 1;
6+
7+
for (let i=1; i<s.length; i++) {
8+
if (s[i] !== "0") {
9+
dp[i] += dp[i-1];
10+
}
11+
12+
const two = Number(s[i-1] + s[i]);
13+
14+
if (10 <= two && two <= 26) {
15+
if (i === 1) {
16+
dp[i] += 1;
17+
} else {
18+
dp[i] += dp[i-2];
19+
}
20+
}
21+
}
22+
return dp[s.length - 1];
23+
};

0 commit comments

Comments
 (0)