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 ...
随机推荐
- BPEL 实例教程
http://www.oracle.com/technetwork/cn/articles/matjaz-bpel1-090722-zhs.html BPEL 实例教程 作者:Matjaz Juric ...
- scrapy1_官网教程
https://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html 本篇文章主要介绍如何使用编程的方式运行Scrapy爬虫. 在开始本文之 ...
- rem是如何实现自适应布局的?
http://caibaojian.com/web-app-rem.html 使用rem 然后根据媒体查询实现自适应.跟使用JS来自适应适配也是同个道理,不过是js更精确一点.使用媒体查询: html ...
- .NET Reflector Visual Studio Extension
https://visualstudiogallery.msdn.microsoft.com/95789cdb-08f9-4dae-9b2f-fc45a452ad77/
- Unity3D PerRendererData
http://nordicedu.com/tkokblog/wordpress/?tag=perrendererdata MaterialPropertyBlock and SpriteRendere ...
- Windows Platform Predefined Macros
https://msdn.microsoft.com/en-us/library/b0084kay.aspx
- 分享一个控制JS 浏览器缓存的解决办法。
JS 缓存的问题一直都是我们又爱又恨的东西.也是我们比较头痛的问题, 一方面为了提高网站响应速度,减少服务器的负担,和节省带宽,将需要将静态资源缓存在客户端, 但是另一方面,当js 文件有改动的时候 ...
- Could not load file or assembly Microsoft.Web.Infrastructure
Error info:Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=n ...
- 创建守护进程步骤与setsid() -- linux deamon进程
原创:http://www.cnblogs.com/mickole/p/3188321.html 一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且 ...
- C#程序
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...