Skip to content

Commit 91965ef

Browse files
committed
Add num decoding ways solution
1 parent 0252624 commit 91965ef

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package java1.algorithms.dynamicProgramming;
2+
3+
public class NumDecodingWays {
4+
// TC:O(n) SC:O(1)
5+
private static int numDecodingWays1(String str) {
6+
if((str.length() == 0) || (str.charAt(0) == '0')) return 0;
7+
8+
if(str.length() == 1) return 1;
9+
10+
int prevCount1 = 1, prevCount2 = 1;
11+
12+
for(int i=2; i<=str.length(); i++) {
13+
int oneDigit = Integer.valueOf(str.substring(i-1, i));
14+
int twoDigit = Integer.valueOf(str.substring(i-2, i));
15+
int count = 0;
16+
17+
if(oneDigit >=1) {
18+
count += prevCount2;
19+
}
20+
if(twoDigit >=10 && twoDigit <=26) {
21+
count += prevCount1;
22+
}
23+
prevCount1 = prevCount2;
24+
prevCount2 = count;
25+
26+
}
27+
return prevCount2;
28+
}
29+
30+
// TC:O(n) SC:O(n)
31+
private static int numDecodingWays2(String str) {
32+
if(str.charAt(0) == '0') return 0;
33+
int[] dp = new int[str.length()+1];
34+
dp[0] = 1;
35+
dp[1] = str.charAt(0) == '0' ? 0 : 1;
36+
37+
for(int i=2; i<=str.length(); i++) {
38+
int oneDigit = Integer.valueOf(str.substring(i-1, i));
39+
int twoDigit = Integer.valueOf(str.substring(i-2, i));
40+
41+
if(oneDigit >=1) {
42+
dp[i] += dp[i-1];
43+
}
44+
if(twoDigit >= 10 && twoDigit <=26) {
45+
dp[i] += dp[i-2];
46+
}
47+
}
48+
return dp[str.length()];
49+
}
50+
51+
public static void main(String[] args) {
52+
String str1 = "221";
53+
String str2 = "06";
54+
System.out.println(numDecodingWays1(str1));
55+
System.out.println(numDecodingWays1(str2));
56+
System.out.println(numDecodingWays2(str1));
57+
System.out.println(numDecodingWays2(str2));
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// TC:O(n) SC:O(1)
2+
function numDecodingWays1(str){
3+
if(str.length === 0 || str.charAt(0) === '0') return 0;
4+
if(str.length === 1) return 1;
5+
6+
let prevCount1 = prevCount2 = 1;
7+
for(let i=2; i<= str.length; i++) {
8+
let oneDigit = Number.parseInt(str.substr(i-1, i));
9+
let twoDigit = Number.parseInt(str.substr(i-2, i));
10+
let count = 0;
11+
12+
if(oneDigit >=1) {
13+
count += prevCount2;
14+
}
15+
16+
if(twoDigit >=10 && twoDigit <=26) {
17+
count += prevCount1;
18+
}
19+
20+
prevCount1 = prevCount2;
21+
prevCount2 = count;
22+
}
23+
return prevCount2;
24+
}
25+
26+
// TC:O(n) SC:O(n)
27+
function numDecodingWays2(str){
28+
let dp = new Array(str.length+1).fill(0);
29+
30+
dp[0] = 1;
31+
dp[1] = str.charAt(0) === '0' ? 0 : 1;
32+
33+
for(let i=2; i<= str.length; i++) {
34+
let oneDigit = Number.parseInt(str.substr(i-1, i));
35+
let twoDigit = Number.parseInt(str.substr(i-2, i));
36+
37+
if(oneDigit >=1) {
38+
dp[i] += dp[i-1];
39+
}
40+
41+
if(twoDigit >=10 && twoDigit <=26) {
42+
dp[i] += dp[i-2];
43+
}
44+
}
45+
return dp[str.length];
46+
}
47+
48+
let str1 = "221";
49+
let str2 = "06";
50+
console.log(numDecodingWays1(str1));
51+
console.log(numDecodingWays1(str2));
52+
console.log(numDecodingWays2(str1));
53+
console.log(numDecodingWays2(str2));

0 commit comments

Comments
 (0)