-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaystoDecode.cpp
More file actions
84 lines (67 loc) · 1.55 KB
/
WaystoDecode.cpp
File metadata and controls
84 lines (67 loc) · 1.55 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Ways to Decode
Asked in:
Facebook
Amazon
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
Input Format:
The first and the only argument is a string A.
Output Format:
Return an integer, representing the number of ways to decode the string.
Constraints:
1 <= length(A) <= 1e5
Example :
Input 1:
A = "8"
Output 1:
1
Explanation 1:
Given encoded message "8", it could be decoded as only "H" (8).
The number of ways decoding "8" is 1.
Input 2:
A = "12"
Output 2:
2
Explanation 2:
Given encoded message "12", it could be decoded as "AB" (1, 2) or "L" (12).
The number of ways decoding "12" is 2.
Seen this question in a real interview before
*/
long dp[100010];
long foo(int ai,string &A)
{
if(ai>A.size())
return 0;
if(ai==A.size())
return 1;
if(dp[ai]!=-1)
return dp[ai];
if(A[ai]=='0')
{
return 0;
}
if(A[ai]=='1')
{
if(ai!=A.size()-1 && A[ai+1]==0)
return dp[ai]=foo(ai+2,A);
return dp[ai]=foo(ai+1,A)+foo(ai+2,A);
}
if(A[ai]=='2')
{
if(ai!=A.size()-1 && A[ai+1]==0)
return dp[ai]=foo(ai+2,A);
if(ai!=A.size()-1 && A[ai+1]<='6')
return dp[ai]=foo(ai+1,A)+foo(ai+2,A);
return dp[ai]=foo(ai+1,A);
}
return dp[ai]=foo(ai+1,A);
}
int Solution::numDecodings(string A) {
memset(dp,-1,sizeof(dp));
return foo(0,A);
}