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 ...
随机推荐
- NoSQL数据库一MongoDB基本使用
如今的网站对数据存储要求越来越灵活,在这种需求下 NoSQL 也就是非关系数据库越来越流行.所谓非关系数据库,是指不使用 SQL 语言进行数据操作的数据库的统称.这类数据库存储数据时没有固定的模式,不 ...
- Spring IoC 详解(四)
Spring IoC 概述 IoC:Inverse of Control(控制反转) 为了更好的理解用可以这样通俗易懂的这样讲:IOC主要是说是new一个类来使用,方式分为:开发这手动创建和Sprin ...
- VC++实现遍历指定文件夹
VC++实现遍历指定文件夹,并进行深度遍历,一级,二级...最终列出该文件夹下所有文件全路径. #include "stdafx.h" #include <iostream& ...
- web服务器/HTTP协议基础
1.http协议:一种规范和约定,实现客户端和服务器的通信2.http请求格式:请求行+请求头+请求体 请求行:method + request-URI + http-version 方法+请求的资源 ...
- Redis慢日志取出来
http://blog.chinaunix.net/uid-31396856-id-5758295.htmlhttps://blog.51cto.com/legehappy/2151986?sourc ...
- ActiveMQ 消息存储持久化
ActiveMQ提供了一个插件式的消息存储,类似于消息的多点传播,主要实现了如下几种: AMQ消息存储-基于文件的存储方式,是以前的默认消息存储 KahaDB消息存储-提供了容量的提升和恢复能力,是现 ...
- ASM实例修改SYS密码
修改ASM实例中SYS用户密码 How To Change ASM SYS PASSWORD ? (文档 ID 452076.1) Oracle Database - Enterprise Editi ...
- Linux Mysql 备份与还原
1. 备份 cd /var/lib/mysql //进入到MySQL库目录 mysqldump -u root -p 数据库>/root/backup/数据库.sql 然后输入密码 2. 还原 ...
- 自动化测试之if __name__ == '__main__'未运行
自动化测试之if __name__ == '__main__'未运行 添加Count类 calculator.py: class Count: def __init__(self,a,b): self ...
- ASP.NET MVC或者.net Core mvc 页面使用富文本控件的 保存问题
https://blog.csdn.net/leftfist/article/details/69629394 目前在做的项目存在XSS安全漏洞! 原因是有一些页面使用了富文本编辑框,为了使得其内容可 ...