-
Notifications
You must be signed in to change notification settings - Fork 2
/
091_Decode_Ways.cpp
54 lines (44 loc) · 1.57 KB
/
091_Decode_Ways.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
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
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.
For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
The number of ways decoding "12" is 2.
*/
// Dynamic Programming with rolling array space optimization. Time: O(n), Space: O(1).
class Solution {
public:
int numDecodings(string s) {
if (s.empty()) return 0;
if (s.size() == 1) return validOne(s[0]);
int n = s.size(); // now n >= 2
vector<int> dp(3, 0); // dp[i] = number of decode ways for s[0:i]
// initialize dp[0]
if (!validOne(s[0])) return 0;
dp[0] = 1;
if (validOne(s[1])) {
dp[1] = dp[0];
if (validTwo(s[0], s[1])) dp[1]++;
} else {
if (validTwo(s[0], s[1])) dp[1] = 1;
else return 0;
}
for (int i = 2; i < n; i++) {
if (validOne(s[i]) && validTwo(s[i-1], s[i])) dp[i%3] = dp[(i-1)%3] + dp[(i-2)%3];
if (validOne(s[i]) && !validTwo(s[i-1], s[i])) dp[i%3] = dp[(i-1)%3];
if (!validOne(s[i]) && validTwo(s[i-1], s[i])) dp[i%3] = dp[(i-2)%3];
if (!validOne(s[i]) && !validTwo(s[i-1], s[i])) return 0;
}
return dp[(n-1)%3];
}
bool validOne(char c) {
return c >= '1' && c <= '9';
}
bool validTwo(char a, char b) {
return a == '1' || (a == '2' && b <= '6');
}
};