-
Notifications
You must be signed in to change notification settings - Fork 64
/
CountPossibleDecodings.java
55 lines (45 loc) · 1.22 KB
/
CountPossibleDecodings.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
/**
* URL: http://www.geeksforgeeks.org/count-possible-decodings-given-digit-sequence/
* Question:
* Let 1 represent ‘A’, 2 represents ‘B’, etc. Given a digit sequence, count the
* number of possible decodings of the given digit sequence.
* E.g.
* Input: digits[] = "121"
* Output: 3
* The possible decodings are "ABA", "AU", "LA"
*
* Input: digits[] = "1234"
* Output: 3
* The possible decodings are "ABCD", "LCD", "AWD"
*/
public class CountPossibleDecodings {
int num;
Integer[] mem;
CountPossibleDecodings(int num){
this.num = num;
mem = new Integer[num + 1];
}
int solve(int n){
if (mem[n] != null) return mem[n];
// base case:
if (n < 10) return 1;
int ans = 0;
// if last digit forms an alphabet
if (n%10 > 0)
ans += solve(n/10); // use last digit as character
// if last 2 digits forms a alphabet and does not have leading 0
if (n%100 <= 26 && n%100 > 9)
ans += solve(n/100); // use last 2 digits as character
mem[n] = ans;
return ans;
}
void run(){
System.out.println(solve(num));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
(new CountPossibleDecodings(sc.nextInt())).run();
sc.close();
}
}