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. (一)Knockout 计算属性

    1 Computed 首先,创建一个view model如下: <body> <p>The fullname is: <span data-bind="text ...

  2. windows和linux换行规则的区别

    在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符.但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字符.要是在这 ...

  3. arraylist 为什么 删除元素时要使用迭代器而不能使用遍历

    因为你要是遍历了,arraylist 的长度就变了,容易数组越界和下标问题 public class Test {     public static void main(String[] args) ...

  4. 动态规划——Valid Permutations for DI Sequence

    We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...

  5. 潭州课堂25班:Ph201805201 tornado 项目 第五课 增加用户系统-用户中心(课堂笔记)

    tornado 相关说明 在 users 表中创建记录,做测试 在项目根目录下创建 test.py # -*- coding: utf-8 -*- # 斌彬电脑 # @Time : 2019/2/27 ...

  6. 3D Slicer Hide 3D Cube and Axis Labels Programmatically 使用代码隐藏三维视图中的方框和坐标轴标签

    在3D Slicer中,我们如果想在自己写的插件中来修改三维视图中的默认设置的话,那么首先就需要获得三维视图的结点,其类型为vtkMRMLViewNode,获得了这个结点后,我们就可以用代码来修改一系 ...

  7. oracle统计数据时,涉及两个表的数据

    SELECT t1.*,a.num FROM (SELECT SUM(t.total_profit) total_profit, SUM(t.main_business_income) main_bu ...

  8. Spring-注入

    一.Spring的基本介绍:Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建.简单来说,Spring是一个分层的JavaSE/ ...

  9. 出现errSecInternalComponent

    出现errSecInternalComponent Xcode签名机制(code signing mechanism) 的 bug, Xcode 中账号多了,就会产生很多过期的描述文件,Xcode 没 ...

  10. mock server 实现get方法的接口(二)

    mock server 实现get方法的接口(二) 下面是实现查询品牌的接口demo: 1.当response数据量小的时候,可以直接使用json, mock会自动设置headers为applicat ...