当前位置:网站首页>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 .
边栏推荐
- [opencv450] hog+svm and hog+cascade for pedestrian detection
- Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
- How to determine whether the current script is in the node environment or the browser environment?
- sso单点登录的实现。
- Excel PivotTable
- Node -- add compressed file
- gradle
- [bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- 【js通过url下载文件】
猜你喜欢

Review notes of compilation principles

Leetcode question brushing: stack and queue 07 (maximum value of sliding window)

Weather forecast applet source code weather wechat applet source code

Excel PivotTable

The new version of graphic network PDF will be released soon

The 8-year salary change of testers makes netizens envy it: you pay me one year's salary per month

AIX存储管理之卷组属性的查看和修改(二)

Output results of convolution operation with multiple tensors and multiple convolution kernels

2022 operation of simulated examination platform for melting welding and thermal cutting work license
![[eight sorts ①] insert sort (direct insert sort, Hill sort)](/img/8d/2c45a8fb582dabedcd2658cd7c54bc.png)
[eight sorts ①] insert sort (direct insert sort, Hill sort)
随机推荐
RFID makes the inventory of fixed assets faster and more accurate
What skills does an excellent software tester need to master?
Picture puzzle wechat applet source code_ Support multi template production and traffic master
If the browser is accidentally closed, how does react cache the forms filled out by users?
2022 low voltage electrician examination questions and answers
[opencv450] hog+svm and hog+cascade for pedestrian detection
JS common library CDN recommendation
Leetcode skimming: stack and queue 02 (realizing stack with queue)
Excel search and reference function
Global and Chinese markets for maritime services 2022-2028: Research Report on technology, participants, trends, market size and share
Excel PivotTable
Cookie, session, tooken
Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
2023 Lexus ES products have been announced, which makes great progress this time
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
Global and Chinese market of wireless charging magnetic discs 2022-2028: Research Report on technology, participants, trends, market size and share
2022拼多多详情/拼多多商品详情/拼多多sku详情
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
Common loss function of deep learning