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. Python.__getattr__Vs__getattribute__

    __getattr__ Vs __getattribute__ class Fish(object): def __getattr__(self, key): if key == 'color': p ...

  2. 那些我离不开的 Sketch 插件

    当谈论到插件时,我是一名极客.各种新颖的 Sketch 插件层出不穷,但是有那么几个是我怎么也离不开的. Sketch 运行器 多层的插件菜单再也不会影响我的效率了. 我推迟了好几年才使用这个插件,因 ...

  3. Jmeter中正则表达式不区分大小写进行匹配

    (?i)<r i="([A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12})" ...

  4. 学习类App原型制作分享-Wokabulary

    Wokabulary是一款多功能词汇学习App,可以学习多国语言词汇.原型的引导页面采用的图片+文字+分页器,需要注意的是分页器选中位置要与页面顺序一致.其次是语言的选择页面,在前面给大家介绍过滚动区 ...

  5. oracle 用一个表的一个字段更新另一个表的一个字段

    案列: 想更新A表的name字段,由于失误,在写这个表的时候,这个字段没有写,发现的时候,已经写了一个多月的数据了.改了之后的过程,会正常的写这个字段, 可是已经写了的数据也不能铲了,重新计算. 好在 ...

  6. 2018.09.28 bzoj3688: 折线统计(dp+树状数组)

    传送门 简单树状数组优化dp. 注意到k很小提示我们搜(d)(d)(d)索(p)(p)(p). 先按第一维排序. 用f[i][j][0/1]f[i][j][0/1]f[i][j][0/1]表示第i个点 ...

  7. 2018.09.10 loj#10172. 涂抹果酱(状压dp)

    传送门 三进制状压感觉有点难写啊. 不过这题状态转移方程挺简单的. 就直接f[i][j]表示前i行第i行状态为j时的选法总数,分情况转移就行了. 代码: #include<bits/stdc++ ...

  8. Linux服务器部署系列之四—DHCP篇

    DHCP服务器的配置是Linux服务器配置中最简单的服务之一,网上也有很多相关文档,不过大部分都只是讲解了配置.虽然我这篇文档也不一定很完善,不过我还是希望能够尽量说得明白一些,同时也希望大家能够提供 ...

  9. python3.4用循环往mysql5.7中写数据并输出

    #!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...

  10. Java 增强 for 循环

    Java 增强 for 循环 Java5 引入了一种主要用于数组的增强型 for 循环. Java 增强 for 循环语法格式如下: for(声明语句 : 表达式) { //代码句子 } 声明语句:声 ...