当前位置:网站首页>969 interlaced string
969 interlaced string
2022-07-02 00:57:00 【-Lin Zeyu】
The title is as follows
Given three strings s1、s2、s3, Please help to verify s3 Is it by s1 and s2 staggered Composed of .
Two strings s and t staggered The definition and process are as follows , Each of these strings is split into several Non empty Substring :
s = s1 + s2 + … + sn
t = t1 + t2 + … + tm
|n - m| <= 1
staggered yes s1 + t1 + s2 + t2 + s3 + t3 + … perhaps t1 + s1 + t2 + s2 + t3 + s3 + …
Be careful :a + b It means string a and b Connect .
Example 1:
Input :s1 = “aabcc”, s2 = “dbbca”, s3 = “aadbbcbcac”
Output :true
Example 2:
Input :s1 = “aabcc”, s2 = “dbbca”, s3 = “aadbbbaccc”
Output :false
Example 3:
Input :s1 = “”, s2 = “”, s3 = “”
Output :true
Tips :
0 <= s1.length, s2.length <= 100
0 <= s3.length <= 200
s1、s2、 and s3 It's all made up of lowercase letters
How to solve the problem 1
Dynamic programming 

class Solution
{
public:
bool isInterleave(string s1, string s2, string s3)
{
auto f = vector < vector <int> >(s1.size() + 1, vector <int>(s2.size() + 1, false));
int n = s1.size(), m = s2.size(), t = s3.size();
if (n + m != t)
{
return false;
}
f[0][0] = true;
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= m; ++j)
{
int p = i + j - 1;
if (i > 0)
{
f[i][j] |= (f[i - 1][j] && s1[i - 1] == s3[p]);
}
if (j > 0)
{
f[i][j] |= (f[i][j - 1] && s2[j - 1] == s3[p]);
}
}
}
return f[n][m];
}
};
It is not difficult to see that the time complexity and space complexity of this implementation are O(nm).
Use rolling arrays to optimize spatial complexity . Because the array here ff Of the ii Line only and No i - 1i−1 Row correlation , So we can optimize this dynamic programming with rolling array , In this way, the spatial complexity can become O(m).
An optimization method
class Solution
{
public:
bool isInterleave(string s1, string s2, string s3)
{
auto f = vector <int>(s2.size() + 1, false);
int n = s1.size(), m = s2.size(), t = s3.size();
if (n + m != t)
{
return false;
}
f[0] = true;
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= m; ++j)
{
int p = i + j - 1;
if (i > 0)
{
f[j] &= (s1[i - 1] == s3[p]);
}
if (j > 0)
{
f[j] |= (f[j - 1] && s2[j - 1] == s3[p]);
}
}
}
return f[m];
}
};
Complexity analysis
Time complexity :O(nm), The time cost of the double cycle is O(nm).
Spatial complexity :O(m), namely s 2 The length of .
边栏推荐
- Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)
- SSO single sign on implementation.
- 【opencv】train&test HOG+SVM
- How does schedulerx help users solve the problem of distributed task scheduling?
- JS common library CDN recommendation
- Summary of Aix storage management
- Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
- Cookie, session, tooken
- cookie、session、tooken
- Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
猜你喜欢

【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)

Kuberntes cloud native combat high availability deployment architecture

Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)

Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners

RFID让固定资产盘点更快更准

Powerful calendar wechat applet source code - support the main mode of doing more traffic

Slf4j print abnormal stack information

Creating logical volumes and viewing and modifying attributes for AIX storage management

Leetcode skimming: stack and queue 03 (valid parentheses)

With the acquisition of Xilinx, AMD is more than "walking on two legs" | Jiazi found
随机推荐
449-原码、补码、反码
export default 导出的对象,不能解构问题,和module.exports的区别
Recently, three articles in the nature sub Journal of protein and its omics knowledge map have solved the core problems of biology
【CTF】bjdctf_ 2020_ babystack2
2022 safety officer-b certificate examination practice questions simulated examination platform operation
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
【js通过url下载文件】
449 original code, complement code, inverse code
From 20s to 500ms, I used these three methods
Global and Chinese markets for power over Ethernet (POE) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
SSO single sign on implementation.
Node - generate wechat permission verification configuration
Node——Egg 创建本地文件访问接口
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
Friends circle community program source code sharing
Leetcode skimming: stack and queue 01 (realizing queue with stack)
[leetcode] number of maximum consecutive ones
Barbie q! How to analyze the new game app?
Leetcode question brushing: stack and queue 07 (maximum value of sliding window)