[LeetCode] 97. Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
给定字符串s1, s2, s3,求s3是否可以由s1和s2交错形成。
解法:DP动态规划,
递推公式为:
dp[i][j] = (dp[i - 1][j] && s1[i - 1] == s3[i - 1 + j]) || (dp[i][j - 1] && s2[j - 1] == s3[j - 1 + i]);
其中dp[i][j] 表示的是 s2 的前 i 个字符和 s1 的前 j 个字符是否匹配 s3 的前 i+j 个字符
Java:
public boolean isInterleave(String s1, String s2, String s3) {
if ((s1.length()+s2.length())!=s3.length()) return false;
boolean[][] matrix = new boolean[s2.length()+1][s1.length()+1];
matrix[0][0] = true;
for (int i = 1; i < matrix[0].length; i++){
matrix[0][i] = matrix[0][i-1]&&(s1.charAt(i-1)==s3.charAt(i-1));
}
for (int i = 1; i < matrix.length; i++){
matrix[i][0] = matrix[i-1][0]&&(s2.charAt(i-1)==s3.charAt(i-1));
}
for (int i = 1; i < matrix.length; i++){
for (int j = 1; j < matrix[0].length; j++){
matrix[i][j] = (matrix[i-1][j]&&(s2.charAt(i-1)==s3.charAt(i+j-1)))
|| (matrix[i][j-1]&&(s1.charAt(j-1)==s3.charAt(i+j-1)));
}
}
return matrix[s2.length()][s1.length()];
}
Python:
# O(m*n) space
def isInterleave1(self, s1, s2, s3):
r, c, l= len(s1), len(s2), len(s3)
if r+c != l:
return False
dp = [[True for _ in xrange(c+1)] for _ in xrange(r+1)]
for i in xrange(1, r+1):
dp[i][0] = dp[i-1][0] and s1[i-1] == s3[i-1]
for j in xrange(1, c+1):
dp[0][j] = dp[0][j-1] and s2[j-1] == s3[j-1]
for i in xrange(1, r+1):
for j in xrange(1, c+1):
dp[i][j] = (dp[i-1][j] and s1[i-1] == s3[i-1+j]) or \
(dp[i][j-1] and s2[j-1] == s3[i-1+j])
return dp[-1][-1]
Python:
# O(2*n) space
def isInterleave2(self, s1, s2, s3):
l1, l2, l3 = len(s1)+1, len(s2)+1, len(s3)+1
if l1+l2 != l3+1:
return False
pre = [True for _ in xrange(l2)]
for j in xrange(1, l2):
pre[j] = pre[j-1] and s2[j-1] == s3[j-1]
for i in xrange(1, l1):
cur = [pre[0] and s1[i-1] == s3[i-1]] * l2
for j in xrange(1, l2):
cur[j] = (cur[j-1] and s2[j-1] == s3[i+j-1]) or \
(pre[j] and s1[i-1] == s3[i+j-1])
pre = cur[:]
return pre[-1]
Python:
# O(n) space
def isInterleave3(self, s1, s2, s3):
r, c, l= len(s1), len(s2), len(s3)
if r+c != l:
return False
dp = [True for _ in xrange(c+1)]
for j in xrange(1, c+1):
dp[j] = dp[j-1] and s2[j-1] == s3[j-1]
for i in xrange(1, r+1):
dp[0] = (dp[0] and s1[i-1] == s3[i-1])
for j in xrange(1, c+1):
dp[j] = (dp[j] and s1[i-1] == s3[i-1+j]) or (dp[j-1] and s2[j-1] == s3[i-1+j])
return dp[-1]
Python:
# DFS
def isInterleave4(self, s1, s2, s3):
r, c, l= len(s1), len(s2), len(s3)
if r+c != l:
return False
stack, visited = [(0, 0)], set((0, 0))
while stack:
x, y = stack.pop()
if x+y == l:
return True
if x+1 <= r and s1[x] == s3[x+y] and (x+1, y) not in visited:
stack.append((x+1, y)); visited.add((x+1, y))
if y+1 <= c and s2[y] == s3[x+y] and (x, y+1) not in visited:
stack.append((x, y+1)); visited.add((x, y+1))
return False
Python:
# BFS
def isInterleave(self, s1, s2, s3):
r, c, l= len(s1), len(s2), len(s3)
if r+c != l:
return False
queue, visited = [(0, 0)], set((0, 0))
while queue:
x, y = queue.pop(0)
if x+y == l:
return True
if x+1 <= r and s1[x] == s3[x+y] and (x+1, y) not in visited:
queue.append((x+1, y)); visited.add((x+1, y))
if y+1 <= c and s2[y] == s3[x+y] and (x, y+1) not in visited:
queue.append((x, y+1)); visited.add((x, y+1))
return False
Python:
# Time: O(m * n)
# Space: O(m + n)
class Solution(object):
# @return a boolean
def isInterleave(self, s1, s2, s3):
if len(s1) + len(s2) != len(s3):
return False
if len(s1) > len(s2):
return self.isInterleave(s2, s1, s3)
match = [False for i in xrange(len(s1) + 1)]
match[0] = True
for i in xrange(1, len(s1) + 1):
match[i] = match[i -1] and s1[i - 1] == s3[i - 1]
for j in xrange(1, len(s2) + 1):
match[0] = match[0] and s2[j - 1] == s3[j - 1]
for i in xrange(1, len(s1) + 1):
match[i] = (match[i - 1] and s1[i - 1] == s3[i + j - 1]) \
or (match[i] and s2[j - 1] == s3[i + j - 1])
return match[-1]
Python:
# Time: O(m * n)
# Space: O(m * n)
# Dynamic Programming
class Solution2(object):
# @return a boolean
def isInterleave(self, s1, s2, s3):
if len(s1) + len(s2) != len(s3):
return False
match = [[False for i in xrange(len(s2) + 1)] for j in xrange(len(s1) + 1)]
match[0][0] = True
for i in xrange(1, len(s1) + 1):
match[i][0] = match[i - 1][0] and s1[i - 1] == s3[i - 1]
for j in xrange(1, len(s2) + 1):
match[0][j] = match[0][j - 1] and s2[j - 1] == s3[j - 1]
for i in xrange(1, len(s1) + 1):
for j in xrange(1, len(s2) + 1):
match[i][j] = (match[i - 1][j] and s1[i - 1] == s3[i + j - 1]) \
or (match[i][j - 1] and s2[j - 1] == s3[i + j - 1])
return match[-1][-1]
Python:
# Time: O(m * n)
# Space: O(m * n)
# Recursive + Hash
class Solution3(object):
# @return a boolean
def isInterleave(self, s1, s2, s3):
self.match = {}
if len(s1) + len(s2) != len(s3):
return False
return self.isInterleaveRecu(s1, s2, s3, 0, 0, 0) def isInterleaveRecu(self, s1, s2, s3, a, b, c):
if repr([a, b]) in self.match.keys():
return self.match[repr([a, b])] if c == len(s3):
return True result = False
if a < len(s1) and s1[a] == s3[c]:
result = result or self.isInterleaveRecu(s1, s2, s3, a + 1, b, c + 1)
if b < len(s2) and s2[b] == s3[c]:
result = result or self.isInterleaveRecu(s1, s2, s3, a, b + 1, c + 1) self.match[repr([a, b])] = result return result
C++:
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1.length() + s2.length() != s3.length()) return false;
int n1 = s1.length(), n2 = s2.length();
boolean[][] dp = new boolean[n1 + 1][n2 + 1];
for (int i = 0; i <= n1; i++) {
for (int j = 0; j <= n2; j++) {
if (i == 0 && j == 0) { // s1 empty, s2 empty
dp[i][j] = true;
} else {
dp[i][j] = (i > 0 && dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1)) || (j > 0 && dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1));
}
}
}
return dp[n1][n2];
}
}
类似题目:
[LeetCode] 139. Word Break 单词拆分
[LeetCode] 140. Word Break II 单词拆分II
All LeetCode Questions List 题目汇总
[LeetCode] 97. Interleaving String 交织相错的字符串的更多相关文章
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- [leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
- 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 = ...
- Leetcode#97 Interleaving String
原题地址 转化为二维地图游走问题. 比如s1="abab",s2="aab",s3="aabaabb",则有如下地图,其中"^&q ...
- [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】97. 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 ...
- 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 = ...
随机推荐
- 解决 Vue 刷新页面后 store 数据丢失的问题
原来的状态(页面刷新数据会重置) state: { teamA: '主队' }, mutations: { data_teamA(state, x) { state.teamA = x } }, ...
- 使用scrapy框架爬取全书网书籍信息。
爬取的内容:书籍名称,作者名称,书籍简介,全书网5041页,写入mysql数据库和.txt文件 1,创建scrapy项目 scrapy startproject numberone 2,创建爬虫主程序 ...
- 使用python的jira库操作jira的版本单和问题单链接
操作JIRA的API来实现的. 但感觉比单纯操作API要简单一些. from jira import JIRA from django.conf import settings JIRA_URL = ...
- python笔记40-环境迁移freeze生成requirements.txt
前言 我们用python在本地电脑上开发完成一个python自动化项目用例,或者开发完成一个django项目. 需要部署到另外一台电脑或者服务器上的时候,需要导入python相关的依赖包,可以用fre ...
- 动态创建自绘的CListBox注意事项
Create(WS_VISIBLE|WS_CHILD|LBS_NOTIFY|LBS_OWNERDRAWFIXED|LBS_HASSTRINGS|LBS_NOINTEGRALHEIGHT ,rcWnd, ...
- TAPD---“文档”的用途
主要用途:文件的存放 (1)对于测试组:存放测试用例.主要针对当前的迭代,可新建对应的文件夹,上传存放相应的xmind.excel文件.方便开发查找用例文件 (2)对于项目:存放共用的文档等 这里只是 ...
- MYECLIPSE说明书
0. 快捷键================================================================================编辑:Ctrl+Shift+ ...
- WebSocket 实现前后端通信的笔记
之前在做站内信时,用到了 WebSocket ,整理了一些笔记分享如下.本文基于 SpringBoot 2.1.5,本文不涉及环境搭建. 引入依赖 在 Spring 中要使用 WebSocket 功能 ...
- Python爬虫进阶 | 多线程
一.简介 为了提高爬虫程序效率,由于python解释器GIL,导致同一进程中即使有多个线程,实际上也只会有一个线程在运行,但通过request.get发送请求获取响应时有阻塞,所以采用了多线程依然可以 ...
- lyft amundsen简单试用
昨天有说过amundsen 官方为我们提供了dockerc-compose 运行的参考配置,以下是一个来自官方的 quick start clone amundsen 代码 amundsen 使用了g ...