forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecodeWays.java
40 lines (32 loc) · 910 Bytes
/
DecodeWays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class DecodeWays {
// O(n) TC , SC
public int numDecodings(String s) {
int[] dp = new int[s.length()];
if(s.charAt(0) == '0'){
return 0;
}
dp[0] =1;
for(int i=1;i<s.length();i++){
int tens = s.charAt(i-1) -'0';// prev char
int ones = s.charAt(i) -'0';// curr char
if(ones == 0 && tens!=1 && tens!=2){ // 16382284870.......
return 0;
}
int no = getNo(tens,ones);
if(no <=26 && no>=10){
if(i>=2){
dp[i] += dp[i-2];
} else{
dp[i] +=1;
}
}
if(no!=10 && no!=20){
dp[i] =dp[i] + dp[i-1];
}
}
return dp[s.length()-1];
}
private int getNo(int tens, int ones){
return 10*tens + ones;
}
}