Decode Ways II

Description

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 10^9 + 7.

  1. The length of the input string will fit in range [1, 10^5].
  2. The input string will only contain the character * and digits 0 - 9.

    public class Solution {
        /**
         * @param s: a message being encoded
         * @return: an integer
         */
         public int numDecodings(String s) {
            if (s == null || s.length() == 0) {
                return 0;
            }
    
            final int mod = 1000000007;
            int n = s.length();
            int[] f = new int[n + 1];
            f[0] = 1;
            for (int i = 1; i <= n; i++) {
                f[i] = 0;
                if (s.charAt(i - 1) == ‘*‘) {
                    f[i] = (int)((f[i] + 9L * f[i - 1]) % mod);
                    if (i >= 2) {
                        if (s.charAt(i - 2) == ‘*‘) {
                            f[i] = (int)((f[i] + 15L * f[i - 2]) % mod);
                        }
                        else if (s.charAt(i - 2) == ‘1‘) {
                            f[i] = (int)((f[i] + 9L * f[i - 2]) % mod);
                        }
                        else if (s.charAt(i - 2) == ‘2‘) {
                            f[i] = (int)((f[i] + 6L * f[i - 2]) % mod);
                        }
                    }
                }
                else {
                    if (s.charAt(i - 1) != ‘0‘) {
                        f[i] = (f[i] + f[i - 1]) % mod;
                    }
                    if (i >= 2) {
                        if (s.charAt(i - 2) == ‘*‘){
                            if (s.charAt(i - 1) <= ‘6‘) {
                                f[i] = (int)((f[i] + 2L * f[i - 2]) % mod);
                            }
                            else {
                                f[i] = (f[i] + f[i - 2]) % mod;
                            }
                        }
                        else {
                            int twoDigits = (s.charAt(i - 2) - ‘0‘) * 10 + s.charAt(i - 1) - ‘0‘;
                            if (twoDigits >= 10 && twoDigits <= 26) {
                                f[i] = (f[i] + f[i - 2]) % mod;
                            }
                        }
                    }
                }
            }
    
            return f[n];
        }
    }
    

      

原文地址:https://www.cnblogs.com/FLAGyuri/p/12078271.html

时间: 2024-11-04 17:18:04

Decode Ways II的相关文章

leetcode 639 Decode Ways II

首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1]和s[i]decode,如果s[i-1]为*,那么**的组合共有15种(11,12,13.....,21,22..26),注意不是9*9=81种,因为我们是s[i-1]和s[i]一起decode,如果是38这样是大于26的 如果s[i-1]不为*,枚举s[i],组合起来小于26即可 if s[i]!

[Swift]LeetCode639. 解码方法 2 | 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

[email&#160;protected] [91] Decode Ways (Dynamic Programming)

https://leetcode.com/problems/decode-ways/ 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 wa

[LeetCode]91.Decode Ways

题目 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 encode

LeetCode之Decode Ways

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

[LeetCode] Decode Ways [33]

题目 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 enco

【LeetCode】091. Decode Ways

题目: 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 enco

44. Decode Ways &amp;&amp; Gray Code

Decode Ways 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, G

LeetCode: Decode Ways [090]

[题目] 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 en