问题描述:

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

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

算法分析:

“When you see string problem that is about subsequence or matching, dynamic programming method should come to your mind naturally. ”

此题可以用递归,也可以用动态规划。字符串的题一般用动态规划。

dp[i][j]表示s1取前i位,s2取前j位,是否能组成s3的前i+j位

举个列子,注意左上角那一对箭头指向的格子dp[1][1], 表示s1取第1位a, s2取第1位d,是否能组成s3的前两位aa

从dp[0][1] 往下的箭头表示,s1目前取了0位,s2目前取了1位,我们添加s1的第1位,看看它是不是等于s3的第1位,( i + j 位)

从dp[1][0] 往右的箭头表示,s1目前取了1位,s2目前取了0位,我们添加s2的第1位,看看它是不是等于s3的第1位,( i + j 位)

首先第一个条件,新添加的字符,要等于s3里面对应的位( i + j 位),第二个条件,之前那个格子也要等于True

举个简单的例子s1 = ab, s2 = c, s3 = bbc ,假设s1已经取了2位,c还没取,此时是False(ab!=bb),我们取s2的新的一位c,即便和s3中的c相等,但是之前是False,所以这一位也是False

同理,如果s1 = ab, s2 = c, s3=abc ,同样的假设,s1取了2位,c还没取,此时是True(ab==ab),我们取s2的新的一位c,和s3中的c相等,且之前这一位就是True,此时我们可以放心置True (abc==abc)

public class InterleavingString
{
//递归
public boolean isInterleave(String s1, String s2, String s3)
{
if(s1.length() == 0)
{
return s2.equals(s3);//不能用==判断,否则出错
}
if(s2.length() == 0)
{
return s1.equals(s3);
}
if(s3.length() == 0)
{
return s1.length() + s2.length() == 0;
} if(s1.charAt(0) == s3.charAt(0) && s2.charAt(0) != s3.charAt(0))
{
return isInterleave(s1.substring(1), s2, s3.substring(1));
}
else if(s2.charAt(0) == s3.charAt(0) && s1.charAt(0) != s3.charAt(0))
{
return isInterleave(s1, s2.substring(1), s3.substring(1));
}
else if(s1.charAt(0) == s3.charAt(0) && s2.charAt(0) == s3.charAt(0))
{
return isInterleave(s1.substring(1), s2, s3.substring(1)) || isInterleave(s1, s2.substring(1), s3.substring(1));
}
else
{
return false;
}
} //动态规划
public boolean isInterleave2(String s1, String s2, String s3)
{
if(s1 == null || s2 == null || s3 == null) return false;
if(s1.length() + s2.length() != s3.length()) return false;
boolean[][] dp = new boolean[s1.length()+1][s2.length()+1];
dp[0][0] = true;
for(int i = 1; i < s1.length() + 1; i ++)
{
if(s1.charAt(i-1) == s3.charAt(i-1) && dp[i-1][0])
{
dp[i][0] = true;
}
}
for(int j = 1; j < s2.length() + 1; j ++)
{
if(s2.charAt(j-1) == s3.charAt(j-1) && dp[0][j-1])
{
dp[0][j] = true;
}
} for(int i = 1; i < s1.length() + 1; i ++)
{
for(int j = 1; j < s2.length() + 1; j ++)
{
if(s2.charAt(j-1) == s3.charAt(i+j-1) && dp[i][j-1])
{
dp[i][j] = true;
}
if(s1.charAt(i-1) == s3.charAt(i+j-1) && dp[i-1][j])
{
dp[i][j] = true;
}
}
}
return dp[s1.length()][s2.length()];
} public static void main(String[] args)
{
String s1 = "aabcc";
String s2 = "dbbca";
String s3 = "aadbbcbcac";
String s4 = "aadbbbaccc";
InterleavingString lv = new InterleavingString();
System.out.println(lv.isInterleave2(s1, s2, s3));
System.out.println(lv.isInterleave2(s1, s2, s4));
}
}

Interleaving String,交叉字符串,动态规划的更多相关文章

  1. lintcode 中等题:interleaving String 交叉字符串

    题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...

  2. 97. Interleaving String(字符串的交替连接 动态规划)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  3. 097 Interleaving String 交错字符串

    给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...

  4. [LeetCode] Interleaving String - 交织的字符串

    题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...

  5. LeetCode之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  6. 二维动态规划——Interleaving String

    97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...

  7. [LeetCode] Interleaving String [30]

    题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...

  8. 40. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  9. 【一天一道LeetCode】#97. Interleaving String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...

  10. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

随机推荐

  1. Python程序员的10个常见错误(转)

    add by zhj:虽然学Python也有两年了,但这些问题的确容易犯,看来对Python的理解还有些地方不深入.先转了,有时间再好好看 译文:http://blog.jobbole.com/682 ...

  2. 前端 Dom 直接选择器

    文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...

  3. DiskLruCache详解 From GuoLin Blogs.

    作者:郭霖老师,<第一行代码>作者,开源框架LitePal作者 http://blog.csdn.net/guolin_blog/article/details/28863651 概述 记 ...

  4. SDUT1574:组合数的计算

    题目描述 给定n组整数(a,b),计算组合数C(a,b)的值.如C(3,1)=3,C(4,2)=6. 输入 第一行为一个整数n,表示有多少组测试数据.(n <= 100000) 第2-n+1行, ...

  5. CF519 ABCD D. A and B and Interesting Substrings(map,好题)

    A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...

  6. django xadmin的全局配置

    在adminx.py中增加 class BaseSetting(object): enable_themes = True use_bootswatch = True class GlobalSett ...

  7. java通过url抓取网页数据-----正则表达式

    原文地址https://www.cnblogs.com/xiaoMzjm/p/3894805.html [本文介绍] 爬取别人网页上的内容,听上似乎很有趣的样子,只要几步,就可以获取到力所不能及的东西 ...

  8. Selenium之使用PageFactory初始化pageobject

    使用PageFactory初始化pageobject有什么作用呢,下面举个例子来说明 public BaiduPage baiduPage = PageFactory.initElements(dri ...

  9. 如何在mysql中存储音乐和图片文件

    如何在mysql中存储音乐和图片文件? 果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的! 我将告诉你怎样通过HTML表单来储存这些文件, ...

  10. PL/SQL 创建用户及权限操作

    1.创建User create user user01 identified by user01, 2.赋予连接数据库的权限 grant connect to user01; 3.把user00的表E ...