For example we have

'a' -> 1

'b' -> 2

..

'z' -> 26

By given "12", we can decode the string to give result "ab" or 'L', 2 ways to decode, your function should return 2 as an answer.

Now asking by given "1246", what should be the return number;

The thinking process is somehow like this:

by given "1" -> we got 'a'

by given "" -> we got ""

by given "12345" -> 'a' + decode('2345') or 'L' + decode('345'), therefore number of ways to decode "12345"is the same of decode(2345)+decode(345).

Somehow we can see that this is a recursion task, therefore we can use Dynamice Programming + memo way to solve the problem.

const data = "";

function num_ways(data) {
// k : count from last to beginning
function helper(data, k, memo) {
if (k === ) {
// if k equals 0, mean only one single digital number left
// means there must be one char
return ;
} if (data === "") {
// if data equals empty, then return 1
return ;
} if (memo[k] != null) {
return memo[k];
} const start = data.length - k;
if (data[start] === "") {
// if sth start as 0, then no char
return ;
} let result = helper(data, k - , memo); if (k >= && parseInt(data.slice(start, start + ), ) <= ) {
result += helper(data, k - , memo);
} memo[k] = result; return result;
} let memo = [];
return helper(data, data.length, memo);
} const res = num_ways(data);
console.log(res); //

[Algorithm -- Dynamic programming] How Many Ways to Decode This Message?的更多相关文章

  1. [Algorithm -- Dynamic Programming] Recursive Staircase Problem

    For example there is a staricase N = 3 | ---|   |---|    | |---|            | ---|                  ...

  2. Algorithm: dynamic programming

    1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...

  3. [Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16

    For a given array, we try to find set of pair which sums up as the given target number. For example, ...

  4. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  5. 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)

    动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...

  6. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

  7. [Optimization] Dynamic programming

    “就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...

  8. About Dynamic Programming

    Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...

  9. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

随机推荐

  1. Hadamard product

    按元素乘积. python中Hadamard product和matrix product的区分: For numpy.ndarray objects, * performs elementwise ...

  2. Java反射机制Reflection

    Java反射机制 1 .class文件 2 Class类 3 Class类与反射机制 4 Java反射机制的类库支持及简介 5 反射机制的定义与应用 6 反射机制Demo Java反射机制demo(一 ...

  3. Linux下Makefile学习笔记

    makefile 可以用于编译和执行多个C/C++源文件和头文件. (1) #include "file.h" 和 #include <file.h> 的区别 #inc ...

  4. python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  5. luogu P3383 【模板】线性筛素数

    题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...

  6. 入门cout输出的格式(补位和小数精度)

    http://blog.csdn.net/gentle_guan/article/details/52071415   mark一下,妈妈再也不用担心我高精度不会补位了

  7. BZOJ1002: [FJOI2007]轮状病毒 (DP)

    标准做法似乎应该是计算生成树数量的基尔霍夫矩阵之类的.. 我看到的做法是一个神奇的高精度dp,当然以后这个blahblahblah矩阵还是要搞一下..   参考(抄袭)网址   这个dp的原理就是把环 ...

  8. [GCJ2017R2]Fresh Chocolate

    题目大意: 有n个团和很多盒糖,每个团中人数不一定相同,每盒糖中都有p颗糖. 现在要给每个团发糖,要求每个人都要发到糖,只有一盒糖发完后才能发下一盒糖. 发糖的顺序可以任意安排,问经过合理安排后,最多 ...

  9. 基于Java 生产者消费者模式(详细分析)

    Java 生产者消费者模式详细分析 本文目录:1.等待.唤醒机制的原理2.Lock和Condition3.单生产者单消费者模式4.使用Lock和Condition实现单生产单消费模式5.多生产多消费模 ...

  10. PHP文件上传学习

    PHP文件上传学习 <?php // 判断是否有文件上传 if (!isset($_FILES['upfile'])) { die('No uploaded file.'); } // 判断是否 ...