Interleaving String leetcode
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.
定义boolean 数组result[][]表示s1的前i个字符和s2的前j个字符是否能交替组成s3的前i+j个字符。
function:result[i][j] = result[i-1][j] &&(s1[i-1] == s3[i+j-1]) || result[i][j -1] &&(s2[j-1] == s3[i+j-1])
initialize:
result[i][0] = (s1[0...i-1] == s3[0...i-1])
result[0][j] = (s2[0...i-1] == s3[0...i-1])
返回值: result[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;
}
int m = s1.length();
int n = s2.length();
boolean[][] result = new boolean[m + 1][n + 1];
result[0][0] = true;
for (int i = 1; i < m + 1; i++) {
if (result[i - 1][0] && s1.charAt(i - 1) == s3.charAt(i - 1)){
result[i][0] = true;
}
}
for (int j = 1; j < n + 1; j++) {
if (result[0][j - 1] && s2.charAt(j - 1) == s3.charAt(j -1)){
result[0][j] = true;
}
}
for (int i = 1; i < m + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (result[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1) || result[i][j - 1] && s2.charAt(j -1 ) == s3.charAt(i + j - 1)){
result[i][j] = true;
}
}
}
return result[m][n];
}
}
Interleaving String leetcode的更多相关文章
- Interleaving String leetcode java
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...
- Interleaving String——Leetcode
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- [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 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 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 ...
- 40. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
随机推荐
- 深入JVM-锁与并发
一.锁在Java虚拟机中的实现与优化 1.1 偏向锁 偏向锁是JDK 1.6 提出的一种锁优化方式.其核心思想是,如果程序没有竞争,则取消之前已经取得锁的线程同步操作.也就说,若某一锁被线程获取后,便 ...
- nginx访问日志获取访问前10的url
在ELK里面获取top10的url在日志量非常大的情况下是非常消耗内存的,所以写了一个脚本用来快速获取. 配置文件 log.conf [log] log_file = /data/logs/nginx ...
- 《Struts2.x权威指南》学习笔记1
第2章 Struts的hello world 在介绍hello world项目前,文中要求下载和安装Struts2,主要是下载lib库和文档,可用于通过命令行进行代码编译.由于公司采用IntelliJ ...
- Fresnel Effect
http://www.3drender.com/glossary/fresneleffect.htm http://kylehalladay.com/all/graphics/2014/02/23/F ...
- HighCharts学习笔记(一)HighCharts入门
一.HighCharts简介 Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学习.个人网站 ...
- IOS: 账号,证书 好文整理
公钥.私钥.数字证书的概念 http://blog.csdn.net/turui/article/details/2048582 iOS Provisioning Profile(Certificat ...
- Robot Framework--10 万能的evaluate
转自:http://blog.csdn.net/tulituqi/article/details/10124559 这一讲我们重点来介绍一下一个常用的关键字evaluate. 我觉得这个关键字在RF里 ...
- php如何发起POST DELETE GET POST 请求
关于POST,DELETE,GET,POST请求 get:是用来取得数据.其要传递过的信息是拼在url后面,因为其功能使然,有长度的限制 post:是用来上传数据.要上传的数据放在request的he ...
- MV*模式的个人理解
MV*模式主要解决的问题就是 View代码难以维护的问题. MV*模式将View中的逻辑分离出去,形成一个弱逻辑的易于维护的视图. MV*中的*是Model和View的桥梁,负责保持Model和Vie ...
- Orchard源码分析(2):Orchard.Web.MvcApplication类(Global)
概述 分析一个的ASP.NET项目源码,首先可以浏览其项目结构,大致一窥项目其全貌,了解项目之间的依赖关系.其次可以浏览Web.config和Global.asax文件,找到应用程序的入口点. 本 文 ...