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. 洛谷——P2071 座位安排 seat.cpp/c/pas

    P2071 座位安排 seat.cpp/c/pas 题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛, ...

  2. 深入理解正则表达式-----应用于检测csrf的正则表达式

    如何写检测和防御csrf的规则?我们可以利用正则表达式进行匹配.对POST包进行正则匹配,这里只是提供了一个思路. pcre:"/POST \/(?P<uri>.*?) HTTP ...

  3. Centos 7 mysql 安装使用记

    某次把美团云1G 1核 centos 7 装到死机,明白了源码编译安装mysql是个大坑,遂绕路到其他大道. 安装命令 wget http://dev.mysql.com/get/mysql-comm ...

  4. Every-SG 博弈论 mark定义和结论

    http://blog.sina.com.cn/s/blog_51cea4040100h3l9.html 这种类型,可以想成这样,有N组游戏,有N个穿红色衣服的人代表先手,有N个穿蓝色衣服的人代表后手 ...

  5. CodeForces - 1017C The Phone Number

    题面在这里! 一开始有一种构造猜想,可以把答案降到 sqrt(N) 级别. 考虑把 {1,2,...,n} 分成 sqrt(N) 段,每一段是连续的sqrt(N)个数.然后我们倒着把每一段数放上. 比 ...

  6. Java反射机制涉及的类常见方法使用总结

    import java.lang.reflect.Constructor; import java.lang.reflect.*; /*Class:代表一个字节码文件的对象,每当有类被加载进内存,JV ...

  7. Ehcache缓存时间设置

    timeToLiveSeconds和timeToIdleSecondstimeToLiveSeconds=x:缓存自创建日期起至失效时的间隔时间x:timeToIdleSeconds=y:缓存创建以后 ...

  8. js中常用日期时间转换

    常用日期时间处理插件:1. timeago.js处理几分钟之前    2. day.js    3. moment.js 注意: 1. 此处的标准时间格式为  2018-03-23 13:35:47 ...

  9. linux防火墙查看、开启、关闭

    查看   vi /etc/sysconfig/iptables 开启   service iptables stop 关闭   service iptables restart

  10. 应对Memcached缓存失效,导致高并发查询DB的几种思路

    原文地址: http://blog.csdn.net/hengyunabc/article/details/20735701 当Memcached缓存失效时,容易出现高并发的查询DB,导致DB压力骤然 ...