97. Interleaving String(字符串的交替连接 动态规划)
Given:
s1 =
"aabcc",s2 =
"dbbca",When s3 = "aadbbcbcac", return true.When s3 =
"aadbbbaccc", return false.
动态规划
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s3.length() != s1.length() + s2.length()) {
return false;
}
boolean dp[][] = new boolean[s1.length() + 1][s2.length() + 1];
for (int i = 0; i <= s1.length(); i++) {
for (int j = 0; j <= s2.length(); j++) {
if (i == 0 && j == 0) {
dp[i][j] = true;
} else if (i == 0) {
dp[i][j] = dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1);
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1);
} else {
dp[i][j] = (dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
}
return dp[s1.length()][s2.length()];
}
}
97. Interleaving String(字符串的交替连接 动态规划)的更多相关文章
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...
- 【LeetCode】97. Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- [LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2. Example 1: Input: s1 = ...
- [leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
- 动态规划之97 Interleaving String
题目链接:https://leetcode-cn.com/problems/interleaving-string/description/ 参考链接:https://blog.csdn.net/u0 ...
- 97. Interleaving String *HARD* -- 判断s3是否为s1和s2交叉得到的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- leetcode 97 Interleaving String ----- java
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 97. Interleaving String
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 97. Interleaving String (String; DP)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- JavaScript学习系列之内存模型篇
一个热爱技术的菜鸟...用点滴的积累铸就明日的达人 正文 如果真的想学好一门语言,那么一定要了解它内存模型,本篇文章就带你走进JavaScript的内存模型,由于本人才疏学浅,若有什么表述有误的地方, ...
- iOS自动化测试需求实现(iOS按键精灵类似)
需求分析: 作为以需求为驱动的IT公司,有再奇怪的需求都不奇怪,所以“24小时循测第三方应用”这样的需求也可以接受.业务需求重点为: 1.24小时循测 2.无人值守,自动完成 3.自动界面操作(点击. ...
- 利用新浪云平台(SAE) 搭建 HUSTOJ 简易教程
前言: OnlineJudge(OJ)是一种代码在线判定平台,这里有许多的编程题目供你选择,你可以选择题目提交代码,OJ会自动返回你的代码的判定结果.是一种很方便的编程.算法练习平台.详情可见:百度百 ...
- Spring RestTemplate post
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add("auditPara ...
- 自己动手写CPU之第九阶段(2)——载入存储指令说明2(lwl、lwr)
将陆续上传新书<自己动手写CPU>.今天是第38篇,我尽量每周四篇,可是近期已经非常久没有实现这个目标了.一直都有事,不好意思哈. 开展晒书评送书活动,在q=%E4%BA%9A%E9%A9 ...
- Sublime Text2.0.2注冊码
// Sublime Text 3 License Keys // Sublime Text 2.x -– BEGIN LICENSE -– Andrew Weber Single User Lice ...
- SQL SERVER 服务启动失败
好久没用SQL SERVER了.今天启动SQL,发现服务启动失败.报错例如以下:--错误发生 1069-(因为登录失败而无法启动服务.) .百度一下,解决方式例如以下: 请按下列步骤操作: 1.右键单 ...
- Android上几种Animation和多个动画同时播放以ScaleAnimation应用详解
在API Demo的View->Animation下可以找到四个Animation的Demo,第一个3D Translate比较复杂,最后再讲,先讲第2个Interpolator.该Activi ...
- Angular2+学习第3篇 基本知识-组件
一.插值表达式 基本用法与ng1一样. 可以使用 Angular 内置的 json 管道,来显示对象信息,管道用来格式化数据 import { Component } from '@angular/c ...
- 一百本英文原著之旅 ( 15 finished )
记得去年毕业的时候,突然想看英文原著(小说.文学.技术 etc.)来提高自己的英文水平.并且那时候愣愣的有了个宏伟的目标 --> 一百本. 不过也就去年下半年断断续续的看了些页数在200左右的 ...