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.
Direct DFS would result in TLE. Using the method of divide and conquer could solve this problem.
The code seems lengthy, just taking three situations into account as the initial values:
start > end : return 1
start == end && not '0' :
start + 1 ==end && not '01' etc..
The following is the code :
class Solution {
public:
bool isCharacter(string& s, int start, int end) {
int num = 0;
if (start > end)
return false;
else if (start == end) {
if (s[start] >= '1' && s[start] <= '9')
return true;
else
return false;
}
else if (start + 1 == end){
if (s[start] == '1' || (s[start] == '2' && s[end] <= '6'))
return true;
else
return false;
}
}
/*
void countways(string& s, int start) {
if (start >= s.length()) {
++count;
return;
}
for (int i = 0; i <= 1; ++i) {
if (start + i < s.length() && isCharacter(s, start, start + i))
countways(s, start + i +1);
}
}
*/
int countways(string& s, int start, int end) {
int count = 0;
if (start > end)
return 1;
else if (start == end) {
if (isCharacter(s, start, end))
++count;
return count;
}
else if (start + 1 == end) {
if (isCharacter(s, start, end))
++count;
if (isCharacter(s, start, start) && isCharacter(s, end, end))
++count;
return count;
}
int mid = (start + end)/2;
int countl = countways(s, start, mid);
int countr = countways(s, mid+1, end);
count = countl * countr;
if (mid >= 0 && mid +1 <= end && isCharacter(s, mid, mid +1)) {
countl = countways(s, start, mid - 1);
countr = countways(s, mid + 2, end);
count += countl * countr;
}
return count;
}
int numDecodings(string s) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if (s.length() == 0)
return 0;
int count = countways(s, 0, s.length()-1);
return count;
}
};
Python DP Version:
class Solution:
def numDecodings(self, s):
if (self.is_valid_numstr(s) == False):
return 0
slen = len(s)
if (slen <= 0):
return 0
if (slen == 1):
return 1 if self.is_valid_one(s[0]) else 0
f1, f2 = [0 for i in range(slen + 1)], [0 for i in range(slen + 1)]
f1[0], f1[1] = 1 if self.is_valid_one(s[0]) else 0, 1 if self.is_valid_one(s[1]) and self.is_valid_one(s[0]) else 0
f2[0], f2[1] = 0, 1 if self.is_valid_two(s[0:2]) else 0
for i in range(2, slen):
f1[i] = f1[i - 1] + f2[i - 1] if self.is_valid_one(s[i]) else 0
is_last_two_valid = self.is_valid_two(s[(i - 1):(i + 1)])
f2[i] = f2[i - 2] + f1[i - 2] if is_last_two_valid else 0
return f1[slen - 1] + f2[slen - 1]
def is_valid_two(self, s):
if (s[0] == "1" and s[1] >= "0" and s[1] <= "9") or (s[0] == "2" and s[1] >= "0" and s[1] <= "6"):
return True
return False
def is_valid_one(self, ch):
return ch >= "1" and ch <= "9"
def is_valid_numstr(self, s):
for ch in s:
if ch < "0" or ch > "9":
return False
return True
if __name__ == '__main__':
s = Solution()
res = s.numDecodings("01")
print(res)
639. Decode Ways II
Hard
267365FavoriteShare
A message containing letters from A-Z
is being encoded to numbers using the following mapping way:
'A' -> 1 'B' -> 2 ... 'Z' -> 26
Beyond that, now the encoded string can also contain the character '*', which can be treated as one of the numbers from 1 to 9.
Given the encoded message containing digits and the character '*', return the total number of ways to decode it.
Also, since the answer may be very large, you should return the output mod 109 + 7.
Example 1:
Input: "*" Output: 9 Explanation: The encoded message can be decoded to the string: "A", "B", "C", "D", "E", "F", "G", "H", "I".
Example 2:
Input: "1*" Output: 9 + 9 = 18
Note:
- The length of the input string will fit in range [1, 105].
- The input string will only contain the character '*' and digits '0' - '9'.
Python Version:
class Solution:
def count_one(self, s):
if (s[0] == "*"):
return 9
elif (s[0] >= "1" and s[0] <= "9"):
return 1
else:
return 0
def count_two(self, s):
if (s[0] == "*"):
if (s[1] == "*"):
return 15
elif (s[1] >= "0" and s[1] <= "6"):
return 2
elif (s[1] >= "7" and s[1] <= "9"):
return 1
else:
return 0
elif (s[0] == "1" and s[1] >= "0" and s[1] <= "9"):
return 1
elif (s[0] == "2" and s[1] >= "0" and s[1] <= "6"):
return 1
elif (s[0] == "1" and s[1] == "*"):
return 9
elif (s[0] == "2" and s[1] == "*"):
return 6
else:
return 0
def numDecodings(self, s):
slen = len(s)
if (slen <= 0):
return 0
elif (slen == 1):
return self.count_one(s[0])
f1, f2 = [0 for i in range(slen + 1)], [0 for i in range(slen + 1)]
f1[0], f1[1] = self.count_one(s[0]), self.count_one(s[0]) * self.count_one(s[1])
f2[0], f2[1] = 0, self.count_two(s[0:2])
for i in range(2, slen):
f1[i] = ((f1[i - 1] + f2[i - 1]) * self.count_one(s[i]))%1000000007
f2[i] = ((f1[i - 2] + f2[i - 2]) * self.count_two(s[(i - 1):(i + 1)]))%1000000007
return (f1[slen - 1] + f2[slen - 1])%1000000007
s = Solution()
y = s.numDecodings("1*72*")
print(y)