Skip to content

Commit 6bcde67

Browse files
author
jinvicky
committed
decode ways solution
1 parent 0ac5475 commit 6bcde67

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

decode-ways/jinvicky.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int numDecodings(String s) {
3+
if (s.charAt(0) == '0') return 0;
4+
5+
int n = s.length();
6+
int[] dp = new int[n+1];
7+
dp[0] = dp[1] = 1;
8+
9+
for(int i = 2; i <= n; i++) {
10+
int one = Character.getNumericValue(s.charAt(i-1));
11+
int two = Integer.parseInt(s.substring(i-2, i));
12+
13+
if(1 <= one && one <= 9) {
14+
dp[i] += dp[i-1];
15+
}
16+
if(10 <= two && two <= 26) {
17+
dp[i] += dp[i-2];
18+
}
19+
}
20+
21+
return dp[n];
22+
}
23+
}

0 commit comments

Comments
 (0)