forked from uhditya/accordation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecode_ways_Leetcode.cpp
41 lines (38 loc) · 1.04 KB
/
Decode_ways_Leetcode.cpp
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
class Solution
{
public:
int dp[100][2];
// state=0 : ways upto ith index if only ith character is encoded
// state=1 : ways upto ith index if previous character is also included in encoding
int func(int i, int state, string &s)
{
if (i == 0 and state == 0)
return s[i] == '0' ? 0 : 1;
if (i == 0 and state == 1)
return 0;
if (dp[i][state] != -1)
return dp[i][state];
int val;
if (state == 0)
{
if (s[i] == '0')
val = 0;
else
val = func(i - 1, 0, s) + func(i - 1, 1, s);
}
if (state == 1)
{
if (s[i - 1] == '1' or (s[i - 1] == '2' and (s[i] >= '0' and s[i] <= '6')))
val = func(i - 1, 0, s);
else
val = 0;
}
return dp[i][state] = val;
}
int numDecodings(string s)
{
memset(dp, -1, sizeof(dp));
int n = s.length();
return func(n - 1, 0, s) + func(n - 1, 1, s);
}
};