package com.fishercoder.solutions;

/**
 * 639. Decode Ways II
 *
 * 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'.

 */
public class _639 {
    /**reference: https://leetcode.com/articles/decode-ways-ii/#approach-2-dynamic-programming-accepted*/
    int m = 1000000007;

    public int numDecodings(String s) {
        long[] dp = new long[s.length() + 1];
        dp[0] = 1;
        dp[1] = s.charAt(0) == '*' ? 9 : s.charAt(0) == '0' ? 0 : 1;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == '*') {
                dp[i + 1] = 9 * dp[i];
                if (s.charAt(i - 1) == '1') {
                    dp[i + 1] = (dp[i + 1] + 9 * dp[i - 1]) % m;
                } else if (s.charAt(i - 1) == '2') {
                    dp[i + 1] = (dp[i + 1] + 6 * dp[i - 1]) % m;
                } else if (s.charAt(i - 1) == '*') {
                    dp[i + 1] = (dp[i + 1] + 15 * dp[i - 1]) % m;
                }
            } else {
                dp[i + 1] = s.charAt(i) != '0' ? dp[i] : 0;
                if (s.charAt(i - 1) == '1') {
                    dp[i + 1] = (dp[i + 1] + dp[i - 1]) % m;
                } else if (s.charAt(i - 1) == '2' && s.charAt(i) <= '6') {
                    dp[i + 1] = (dp[i + 1] + dp[i - 1]) % m;
                } else if (s.charAt(i - 1) == '*') {
                    dp[i + 1] = (dp[i + 1] + (s.charAt(i) <= '6' ? 2 : 1) * dp[i - 1]) % m;
                }
            }
        }
        return (int) dp[s.length()];
    }
}

@@ -893,19 +893,71 @@
 9 %7B%0A    
-/**
+public static class Solution1 %7B%0A        /**%0A         * 
 referenc
@@ -1044,19 +1044,33 @@
 accepted
-*/%0A
+%0A         */%0A    
     int 
@@ -1086,24 +1086,28 @@
 00007;%0A%0A    
+  
+  
 public int n
@@ -1126,24 +1126,28 @@
 String s) %7B%0A
+    
         long
@@ -1188,16 +1188,20 @@
         
+    
 dp%5B0%5D = 
@@ -1211,16 +1211,20 @@
         
+ 
+   
 dp%5B1%5D = 
@@ -1284,16 +1284,20 @@
         
+    
 for (int
@@ -1323,24 +1323,28 @@
 h(); i++) %7B%0A
+    
             
@@ -1377,32 +1377,36 @@
                 
+    
 dp%5Bi + 1%5D = 9 * 
@@ -1412,16 +1412,20 @@
  dp%5Bi%5D;%0A
+    
         
@@ -1454,32 +1454,36 @@
  - 1) == '1') %7B%0A
+    
                 
@@ -1539,32 +1539,36 @@
                 
+    
 %7D else if (s.cha
@@ -1584,24 +1584,28 @@
 ) == '2') %7B%0A
+    
             
@@ -1665,32 +1665,36 @@
                 
+   
+ 
 %7D else if (s.cha
@@ -1706,32 +1706,36 @@
  - 1) == '*') %7B%0A
+    
                 
@@ -1792,34 +1792,40 @@
                 
-%7D%0A
+    %7D%0A  
             %7D el
@@ -1816,24 +1816,26 @@
             
+  
 %7D else %7B%0A   
@@ -1831,16 +1831,20 @@
  else %7B%0A
+    
         
@@ -1903,32 +1903,36 @@
                 
+    
 if (s.charAt(i -
@@ -1957,32 +1957,36 @@
                 
+ 
+   
 dp%5Bi + 1%5D = (dp%5B
@@ -2002,32 +2002,36 @@
 dp%5Bi - 1%5D) %25 m;%0A
+    
                 
@@ -2101,32 +2101,36 @@
                 
+    
 dp%5Bi + 1%5D = (dp%5B
@@ -2162,32 +2162,36 @@
                 
+ 
+   
 %7D else if (s.cha
@@ -2203,32 +2203,36 @@
  - 1) == '*') %7B%0A
+    
                 
@@ -2299,32 +2299,36 @@
 dp%5Bi - 1%5D) %25 m;%0A
+    
                 
@@ -2345,20 +2345,32 @@
     
-%7D%0A    
     %7D%0A
+            %7D%0A    
     
@@ -2402,16 +2402,26 @@
 gth()%5D;%0A
+        %7D%0A
     %7D%0A%7D%0A

refactor 639