Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

Example 1:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true

Example 2:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
 
这个题目利用dynamic programming,mem[l1 + 1][l2 + 1]  #mem[i][j] means that whether the first i characters of s1 and the first j characters of s2 be able to make first i + j characters of s3. 
  mem[i][j] = (mem[i - 1][j] and s1[i - 1] == s3[i + j - 1])   # when the last character of s3 matches the last of s1
          or (mem[i][j - 1]) and s2[j - 1] == s3[i + j - 1])  # when the last character of s3 matches the last of s2
     initial: mem[i][0] = s1[:i] == s3[:i]
      mem[0][j] = s2[:j] == s3[:j]
 
code
class Solution:
def interLeaveString(self, s1, s2, s3):
l1, l2, l3 = len(s1), len(s2), len(s3)
if l1 + l2 != l3: return False
mem = [[False] * (l2 + 1) for _ in range(l1 + 1)]
for i in range(l1 + 1):
mem[i][0] = s1[:i] == s3[:i]
for j in range(l2 + 1):
mem[0][j] = s2[:j] == s3[:j]
for i in range(1, 1 + l1):
for j in range(1, 1 + l2):
mem[i][j] = (mem[i - 1][j] and s1[i - 1] == s3[i + j - 1]) or (mem[i][j - 1] and s2[j - 1] == s3[i + j - 1])
return mem[l1][l2]
 

[LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  2. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  3. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  4. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  6. [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  9. [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

随机推荐

  1. ionic 3 热更新 Hot Code Push

    最近用ionic 3 做的app业务做的差不多了,突然想到以后app如果有更新该怎么搞?想到我们的app后期更新应该不大,,最多就是改改bug和增加下用户体验,如果只有一些小的更新,然后提交各个应用商 ...

  2. CodeForces 286E Ladies' Shop 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8781889.html 题目传送门 - CodeForces 286E 题意 首先,给你$n$个数(并告诉你$m$ ...

  3. Codeforces 947F. Public Service 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF947F.html 近5K码量构造题,CF血腥残暴! 题解 这里先定义 $FT(k)$ 表示一个菊花树多 k ...

  4. gitLib操作笔录《一》:创建分支,切换分支,提交分支到远程,以及基本代码clone与更新提交到远程操作指令

    git 操作经验注:master表示的是主线,origin 表示远程源 创建分支:git checkout -b < branch_name >或 < master >切换分支 ...

  5. shullfe机制详解

    一.shuffle机制概述 shuffle机制就是发生在MR程序中,Mapper之后,Reducer之前的一系列分区排序的操作.shuffle的作用是为了保证Reducer收到的数据都是按键排序的. ...

  6. Android Studio帮助文档的安装及智能提示设置

    初次使用Android Studio,发现其智能提示不能像Visual Studio一样显示系统方法等的详细用途描述.经查找资料,问题原因是未安装SDK Document. 解决办法如下: 1.打开如 ...

  7. Node.js_简介及其 npm 包管理器基本使用_npm_cnpm_yarn_cyarn

    Node.js 既是语言也是平台,跳过了 Apache.Nginx 等 HTTP 服务器,直接面向前端开发 JavaScript 是由 ECMAScript.文档对象模型(DOM)和浏览器对象模型(B ...

  8. Node.js_express_route 路由

    route 路由   (kiss my ass ヾ(゚∀゚ゞ) 请求方式        get / post /  put / delete____查 / 增 / 改 / 删 路由路径         ...

  9. vue_VueRouter 路由_路由器管理n个路由_并向路由组件传递数据_新标签路由_编程式路由导航

    路由:就是一个 key 与 value 的映射关系.key 就是 pathh 前台路由的 value 是 Component 组件对象 后台路由的 value 是一个 回调函数 普通链接: 会发送请求 ...

  10. [LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...