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. spring开发Eclipse需要做设置

    1. 统一工作空间的编码,选择UTF-8 2. 把创建JSP页面的编码修改UTF-8 3. 重新配置Tomcat服务器 * 先配置Tomcat服务器 * 选择服务器 --> open --> ...

  2. 解决ios手机页面overflow scroll滑动很卡的问题

    在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...

  3. linux-radhat-gitlab服务搭建

    1.安装gitlab的依赖项 sudo yum install -y curl policycoreutils-python openssh-server cronie 2.设置防火墙 sudo lo ...

  4. Codeforces 677C. Coloring Trees dp

    C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  5. redis缓存设置和读取

    一/写入 <?php $redis = new Redis(); //实例化redis $redis->pconnect('); $redis->,'huahua'); //设置变量 ...

  6. UI设计:掌握这6点,轻松0到1

    非科班出身能成为UI设计师吗? 答案是肯定的.世上无难事,只怕有心人.只要找对方法.坚持不懈,即便是零基础也能学好UI设计. 那么零基础学习UI设计,需要学习哪些知识?我们要从哪些地方学起?怎么系统学 ...

  7. 2018.10.14 loj#516. DP 一般看规律(启发式合并)

    传送门 注意到一种颜色改了之后就不能改回去了. 因此可以启发式合并. 每次把小的合并给大的. 这样每个数最多被合并logloglog次. 如果维护一棵比较下标的平衡树的话,对于答案有贡献的就是每个数与 ...

  8. 改变yii2 $form最外层div样式

    <?php $form = ActiveForm::begin([ 'options'=>['class' => 'form-horizontal row-border','enct ...

  9. Java编程模板

    package Campus; import java.util.Scanner; public class Main{ public static void main(String args[]){ ...

  10. MODIS产品分析和数据处理

    ENVI+IDL 17种MODIS产品的功能解释 https://wenku.baidu.com/view/6fd329dcf524ccbff0218440.html ENVI读取MODIS数据大致步 ...