Skip to content

Commit 0306851

Browse files
committed
Add decode ways solution - s0ooo0k
1 parent b8b291a commit 0306851

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

decode-ways/s0ooo0k.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
8+
dp[0]=1;
9+
dp[1]=1;
10+
11+
for(int i=2; i<=n; i++) {
12+
if(s.charAt(i-1)!='0') dp[i]+=dp[i-1];
13+
14+
int group = Integer.parseInt(s.substring(i-2, i));
15+
if(group>=10 && group<=26) {
16+
dp[i]+=dp[i-2];
17+
}
18+
}
19+
return dp[n];
20+
}
21+
}
22+

0 commit comments

Comments
 (0)