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. [转载]mac软件

    效率之王:Afred小帽子:通过前人的配置,替代掉了 有道词典.发音工具.开关机.快速搜索.地图.Spotlight等应用. 主力编辑器:Atom因为高颜值.强大的插件和预览功能 取代了之前的subl ...

  2. POJ poj 2155 Matrix

    题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...

  3. java 软引用,弱引用,强引用

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 强引用 ,有用的对象. 强引用 不会被回收. 软引用,有用 但不是必须的对象. 系统在发 ...

  4. bzoj1116 [POI2008]CLO 边双联通分量

    只需对每个联通块的$dfs$树检查有没有返租边即可 复杂度$O(n + m)$ #include <cstdio> #include <cstring> using names ...

  5. 尝试用Gearman实现分布式处理(PHP)[转]

    本文需要你已对Gearman有个大致了解. 顺便再推荐两篇参考文章http://hi.baidu.com/thinkinginlamp/blog/item/ff49972b9e7378f3e6cd40 ...

  6. Yolov_3 网络结构分析

    转自:https://blog.csdn.net/KKKSQJ/article/details/83587138 original Based on keras-yolov3, understandi ...

  7. bzoj 1067 分情况讨论

    这道题考察人的严谨,各种情况分类讨论. #include <cstdio> #include <algorithm> #include <map> #define ...

  8. 清北学堂学习总结 day2 字符串 练习

    1.hash表(哈希表) codevs 2147 数星星--简单哈希  时间限制: 3 s  空间限制: 64000 KB  题目等级 : 钻石 Diamond 题目描述 Description 小明 ...

  9. Shell基础学习(七) 输入输出重定向

    命令 说明 command>file 将输出重定向到file command<file 将输入重定向到file command >> file 将输出追加到file n > ...

  10. 安卓之service简单介绍

    一 什么是Service 二 如何使用Service 三 Service的生命周期   一 什么是Service Service,看名字就知道跟正常理解的“服务”差不多,后台运行,可交互这样的一个东西 ...