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. Android签名打包详解

    一.      Android签名有什么作用? 应用程序升级:如果你希望用户无缝升级到新的版本,那么你必须用同一个证书进行签名.这是由于只有以同一个证书签名,系统才会允许安装升级的应用程序.如果你采用 ...

  2. Vue 2.0学习(七)方法与事件

    基本用法 以监听一个按钮的点击事件为例,设计一个计数器,每次点击都加1: <div id = "app"> 点击次数:{{ counter }} <button ...

  3. Ubuntu系统 安装谷歌 Chrome 浏览器

    在 Ubuntu 16.04 中安装谷歌 Chrome 浏览器,步骤: 1.sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P / ...

  4. java获取指定日期的年、月、日的值

    参数:String dateStr = '2016-05-18'; 1.获取string对应date日期: Date date = new SimpleDateFormat("yyyy-MM ...

  5. wpf企业应用之UI模块解耦

    关于UI模块的解耦,说简单点,首先需要配置菜单与对应操作类的映射关系(或存放于配置文件,或继承接口直接写死在模块代码中,或存放到数据库,原理都一样),然后在菜单加载时,读取配置项动态生成菜单或是其他控 ...

  6. ACM -- 算法小结(五)字符串算法之Sunday算法

    1. Sunday算法是Daniel M.Sunday于1990年提出的一种比BM算法搜索速度更快的算法. 2. Sunday算法其实思想跟BM算法很相似,只不过Sunday算法是从前往后匹配, 在匹 ...

  7. mybatis源码分析(5)-----拦截器的实现原理(动态代理+责任链)

    写在前面 MyBatsi 的拦截器模式是基于代理的代理模式.并且myBatis 的插件开发也是以拦截器的形式集成到myBatis 当中. MyBatis 的拦截器已经插件是在org.apache.ib ...

  8. 配置Maven环境变量与Intelij IDE配置Maven

    Maven有什么用? 以前我们导入第三方jar包的流程是什么?一般是download,然后copy到项目中,然后依赖(library)项目,最后被我们使用. 通俗的说,就是不用我们自己去downloa ...

  9. HC-07 蓝牙串口模块

    http://www.wavesen.com/probig.asp?id=17 本模块为新推出的产品,各项功能和性能.及引脚封装,均兼容于HC-06. 为低成本需求的的客户推荐本产品.相比HC-06来 ...

  10. 使MySQL对表名不区分大小写

    今天郁闷死了,在LINUX下调一个程序老说找不到表,但是我明明是建了表的,在MYSQL的命令行下也可以查到,为什么程序就找不到表呢? 后来请教了一个老师才搞定,原来是LINUX下的MYSQL默认是要区 ...