Interleaving String (DP)
Given s1, s2, s3, 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.
State:f[i][j] 表示s1的前i 个字符 和 s2的前j 个字符能组成s3的前 i + j 个字符
Function: if (((s1.charAt(i - 1) == s3.charAt(i + j - 1) && interleave[i - 1][j])) || (s2.charAt(j - 1) == s3.charAt(i + j - 1) && interleave[i][j - 1])) {
interleave[i][j] = true;
Initializtion:f[0][0] = true f[i][0] i = (1 ~ s1.length() ) f[0][j] j = (1 ~ s2.length())
Answer:f[s1.length()][s2.length()]
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1.length() + s2.length() != s3.length()) {
return false;
}
boolean[][] interleave = new boolean[s1.length() + 1][s2.length() + 1];
interleave[0][0] = true;
for (int i = 1; i <= s1.length(); i++) {
if (s1.charAt(i - 1) == s3.charAt(i - 1) && interleave[i - 1][0]) {
interleave[i][0] = true;
}
}
for (int i = 1; i <= s2.length(); i++) {
if (s2.charAt(i - 1) == s3.charAt(i - 1) && interleave[0][i - 1]) {
interleave[0][i] = true;
}
}
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
if (((s1.charAt(i - 1) == s3.charAt(i + j - 1) && interleave[i - 1][j])) || (s2.charAt(j - 1) == s3.charAt(i + j - 1) && interleave[i][j - 1])) {
interleave[i][j] = true;
}
}
}
return interleave[s1.length()][s2.length()];
}
}
Interleaving String (DP)的更多相关文章
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 二维动态规划——Interleaving String
97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...
- LeetCode-Interleaving String[dp]
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- LeetCode之“动态规划”:Interleaving String
题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
随机推荐
- [转帖]详解oracle数据库唯一主键SYS_GUID()
详解oracle数据库唯一主键SYS_GUID() https://www.toutiao.com/i6728736163407856139/ 其实 需要注意 这里满不能截取 因为截取了 就不一定唯一 ...
- [转帖]Linux修改时区的正确方法
Linux修改时区的正确方法 /etc/localtime 以及timedatectl 两种方式修改时区. CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后 ...
- django授权-01--oauth2
oauth2的提供商:认证服务器 oauth2的消费者:目标服务器 如果目标服务器与认证服务器不一样的话,相当于目标服务器向认证服务器获取用户的信息 如果目标服务器和认证服务器一样的话,相当于用户获取 ...
- QSqlDatabase
QSqlDatabase 使用静态方法addDatabase来创建一个数据库连接. 如果你的程序中只有一个数据库连接,可以使用如下语句创建连接 QSqlDatabase db = QSqlDatab ...
- C++:标准模板库Sort
一.概述 STL几乎封装了所用的数据结构中的算法,这里主要介绍排序算法的使用,指定排序迭代器区间后,即可实现排序功能. 所需头文件#include <algorithm> sort函数:对 ...
- 1233: 输出杨辉三角前n行(Java)
WUSTOJ 1233: 输出杨辉三角前n行 题目 原题链接 Description 输出杨辉三角前n行. Input 输入一个数n(n <= 9) Output 输出杨辉三角前n行.(注意行末 ...
- EXTI中断开关点亮LED源码
在KEY点亮LED源码的基础上 USER下新建EXIT文件夹,新建bsp_exit.c和bsp_exit.h,添加到工程中(魔术棒添加头文件所在文件夹) bsp_exit.h内容 #ifndef BS ...
- Linux下/etc/login.defs文件
/etc/login.defs文件定义了与/etc/password和/etc/shadow配套的用户限制设定.这个文件是需要的,缺失并不会影响系统的使用,但是也许会产生意想不到的错误. 如果/etc ...
- MH-P虚拟机DSR中安装SQL2008
双击下载好的安装文件setup.exe.(注意:安装之前请确认是否有安装SQL Server 2008 R2需要的.NET Framework 3.5 SP1,我的环境由于之前有配置安装过,在这里不具 ...
- sqlserver使用EF模型经验
sqlserver使用EF模型经验 EF模型使用本人在之前两三年中从没使用过,所以刚开始使用就会踩上许多的坑.今天我不单单说下自己所踩的一些坑与当前公司中使用EF模型设计的理念,即是为我自己做个笔记, ...