All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0601_0700.s0639_decode_ways_ii.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0601_0700.s0639_decode_ways_ii;

// #Hard #String #Dynamic_Programming

public class Solution {
    public int numDecodings(String s) {
        if (s.charAt(0) == '0') {
            return 0;
        }
        long[] dp = new long[s.length() + 1];
        dp[0] = 1;
        dp[1] = s.charAt(0) == '*' ? 9 : 1;
        char[] ch = s.toCharArray();

        for (int i = 2; i <= ch.length; i++) {
            if (ch[i - 1] == '0') {
                if (ch[i - 2] == '1' || ch[i - 2] == '2') {
                    dp[i] = dp[i - 2];
                } else if (ch[i - 2] == '*') {
                    dp[i] = 2 * dp[i - 2];
                } else {
                    return 0;
                }
            } else if (ch[i - 1] >= '1' && ch[i - 1] <= '6') {
                dp[i] = dp[i - 1];
                if (ch[i - 2] == '1' || ch[i - 2] == '2') {
                    dp[i] += dp[i - 2];
                } else if (ch[i - 2] == '*') {
                    dp[i] += 2 * dp[i - 2];
                }
            } else if (ch[i - 1] >= '7' && ch[i - 1] <= '9') {
                dp[i] = dp[i - 1];
                if (ch[i - 2] == '1' || ch[i - 2] == '*') {
                    dp[i] += dp[i - 2];
                }
            } else if (ch[i - 1] == '*') {
                dp[i] = 9 * dp[i - 1];
                if (ch[i - 2] == '1') {
                    dp[i] += 9 * dp[i - 2];
                } else if (ch[i - 2] == '2') {
                    dp[i] += 6 * dp[i - 2];
                } else if (ch[i - 2] == '*') {
                    dp[i] += 15 * dp[i - 2];
                }
            }
            dp[i] %= 1000000007;
        }

        return (int) dp[s.length()];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy