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:

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

Approach #1: DP. [C++]

class Solution {
int mod = 1000000007; public int numDecodings(String s) {
if (s.isEmpty()) return 0;
long[] dp = new long[2];
dp[0] = 1;
dp[1] = ways(s.charAt(0));
// int ans = 0;
for (int i = 1; i < s.length(); ++i) {
long ans = ways(s.charAt(i)) * dp[1] + ways(s.charAt(i-1), s.charAt(i)) * dp[0];
ans %= mod;
dp[0] = dp[1];
dp[1] = ans;
}
return (int)dp[1];
} public int ways(char c) {
if (c == '*') return 9;
if (c == '0') return 0;
return 1;
} public int ways(char c1, char c2) {
if (c1 == '*' && c2 == '*') return 15;
if (c1 == '*')
if (c2 >= '0' && c2 <= '6') return 2;
else return 1;
else if (c2 == '*')
if (c1 == '1') return 9;
else if (c1 == '2') return 6;
else return 0;
else {
int num = (c1 - '0') * 10 + (c2 - '0');
if (num >= 10 && num <= 26) return 1;
else return 0;
} }
}

  

Reference:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-decode-ways-ii/

639. Decode Ways II的更多相关文章

  1. leetcode 639 Decode Ways II

    首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...

  2. [LeetCode] 639. Decode Ways II 解码方法 II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  3. [LeetCode] Decode Ways II 解码方法之二

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  4. [Swift]LeetCode639. 解码方法 2 | Decode Ways II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  5. Decode Ways II

    Description A message containing letters from A-Z is being encoded to numbers using the following ma ...

  6. leetcode 91 Decode Ways I

    令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...

  7. 91. Decode Ways反编译字符串

    [抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  8. [LeetCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  9. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

随机推荐

  1. jQuery控制TR显示隐藏

    参考链接:http://www.jb51.net/article/51221.htm 通过jQuery的hide和show方法即可.

  2. Task构造

    //原文:http://www.tuicool.com/articles/IveiQbQ 创建并且初始化Task 使用lambda表达式创建Task Task.Factory.StartNew(() ...

  3. 7.20 文本框内容 超出 显示 。。 和 split

    word-wrap:break-word; word-break:break-all; overflow:auto; split  去 :等 ,只要有: 就会在:两边 各生产一个值 ,所有 应习惯把最 ...

  4. js实现a_b变成A B的两种方法

    1.var key = 'a_b'; var a = key.replace(/\b.|_./g, function (i) { if (i.length === 2) { i = ' ' + i[1 ...

  5. 2018.06.30 BZOJ 3932: [CQOI2015]任务查询系统(主席树)

    3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec Memory Limit: 512 MB Description 最近实验室正在为其管理的超级计算机编制一套任务管理 ...

  6. 2018.08.05 bzoj3223: Tyvj 1729 文艺平衡树(非旋treap)

    传送门 经典的平衡树问题,之前已经用splay写过一次了,今天我突发奇想,写了一发非旋treap的版本,发现挺好写的(虽然跑不过splay). 代码: #include<bits/stdc++. ...

  7. 25. Green Living 绿色生活

    25. Green Living 绿色生活 ①We all know that humans are damaging the environment,but what can we do about ...

  8. PyCharm2017破解版安装

    PyCharm2017破解版安装步骤: 1.右击软件压缩包选择解压到pycharm2017. 2.在解压文件夹里面找到pycharm-professional-171.3780.47,右击打开. 3. ...

  9. 微分方程数值解Euler法

    微分方程:dy/dt=1+y; 解是y=2exp(x)-1; clc clear figure() dx=0.1; x=:dx:; y=zeros(size(x)); x()=; y()=; :len ...

  10. C语言学生管理系统源码分享

    大家好 我就是如假包换的...陈玲 自从运营了C语言程序设计微信公众号 很多粉丝都给我备注 ...奇葩 实在是不敢当 也被人开始叫玲玲姐 我知道 很多人都想看我出境 我本人也有 年多的舞台演讲训练 实 ...