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 ...
随机推荐
- Jetbrains系列产品2019.2.3最新激活方法
Jetbrains系列产品2019.2.3最新激活方法[持续更新] 发表于 2018-08-25 | 分类于 软件调试 本站惯例:本文假定你知道Jetbrains家的产品.不知道可以问问搜索引擎. 大 ...
- (十三)自定义JSTL标签
前面的博客,我们讲过了 自定义 el函数 : 讲一个 自定义标签技术 : 目录 自定义标签 快速入门:使用标签输出客户机IP 关于标签处理器类的方法 自定义标签功能扩展 传统标签 简单标签 配置简单标 ...
- zookeeper-waches
1. 设置watches data watches: getData() exist() child watches: getChildren() 2. 触发watches setData(): da ...
- 第八讲,TLS表(线程局部存储)
一丶复习线程相关知识 首先讲解TLS的时候,需要复习线程相关知识, (thread local storage ) 1.了解经典同步问题 首先我们先写一段C++代码,开辟两个线程去跑,看看会不会出现 ...
- [yarn]yarn和npm的对比
一.简介 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服 ...
- (七)Redis之持久化之RDB方式
一.持久化概念 所有的数据都存在内存中,从内存当中同步到硬盘上,这个过程叫做持久化过程. 使用方法: 1. rdb持久化方法:在指定的时间间隔写入硬盘 2. aof方式:将以日志,记录 ...
- (十五)SpringBoot之使用Redis做缓存数据
一.添加Redis依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- C#四种深拷贝方法(转载)
原文地址:https://www.cnblogs.com/profession/p/6222489.html //四种深拷贝方法 public static T DeepCopyByReflect&l ...
- aspnet core 全局模型验证,统一api响应
上手就来 新建一个模型验证过滤器,其中ApiResp是自定义的统一响应类. public class VldFilter:IActionFilter { /// <summary> /// ...
- solr 配置中文分析器/定义业务域/配置DataImport功能(测试用)
一.配置中文分析器 使用IKAnalyzer 配置方法: 1)把IK的jar包添加到solr工程中/WEB-INF/lib目录下 2)把IK的配置文件扩展词典, ...