-
Notifications
You must be signed in to change notification settings - Fork 2
/
DecodeWaysII.java
72 lines (64 loc) · 1.63 KB
/
DecodeWaysII.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.algorithm.playground.leetcode.problems.lc600.lc630.lc639;
/**
* https://leetcode.com/problems/decode-ways-ii/
* <p>
* A message containing letters from A-Z is being encoded to numbers using the following mapping way:
* <p>
* '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.
* <p>
* Given the encoded message containing digits and the character '*', return the total number of ways to decode it.
* <p>
* Also, since the answer may be very large, you should return the output mod 10^9 + 7.
*/
public class DecodeWaysII {
class Solution {
private int mod = 1_000_000_007;
public int numDecodings(String s) {
long prev1 = 1, prev2 = 0;
for (int i = s.length() - 1; i >= 0; i--) {
long curr = 0;
char first = s.charAt(i);
if (first != '0') {
if (first == '*') {
curr = 9 * prev1;
} else {
curr = prev1;
}
if (i < s.length() - 1) {
char second = s.charAt(i + 1);
curr += count(first, second) * prev2;
}
curr %= mod;
}
if (curr == 0 && prev1 == 0) {
return 0;
}
prev2 = prev1;
prev1 = curr;
}
return (int) prev1;
}
private int count(char ch1, char ch2) {
if (ch1 == '*' && ch2 == '*') {
return 15;
} else if (ch1 == '*') {
return ch2 <= '6' ? 2 : 1;
} else if (ch2 == '*') {
if (ch1 == '1') {
return 9;
} else if (ch1 == '2') {
return 6;
} else {
return 0;
}
} else {
int val = ((ch1 - '0') * 10) + (ch2 - '0');
return val <= 26 ? 1 : 0;
}
}
}
}