[LeetCode] Interleaving String

简介: This problem can be solved by DP elegantly. You may refer to this link for the code and explanations.

This problem can be solved by DP elegantly. You may refer to this link for the code and explanations.

I try to rewrite the code, also in C++, but find that it can only achieve 8ms, not the fastest. I guess handling 2d vector may be more time-consuming and thus optimize it to be ofO(min(m, n)) space, but the running time does not change (still 8ms).

Finally, I find that replacing vector<bool> dp(m, false); with bool* dp = new bool[m + 1]();reduces the running time to 0ms, which is just what I desire. Well, vector is really slow...

The code is finally as follows (well, I do not release the allocated memory of dp, which is not desirable).

 1 class Solution { 
 2 public:
 3     bool isInterleave(string s1, string s2, string s3) {
 4         int m = s1.length(), n = s2.length(), l = s3.length();
 5         if (m > n) return isInterleave(s2, s1, s3);
 6         if (l != m + n) return false;
 7         bool *dp = new bool[m + 1]();
 8         dp[0] = true;
 9         for (int i = 1; i <= m; i++)
10             dp[i] = dp[i - 1] && s3[i - 1] == s1[i - 1];
11         for (int j = 1; j <= n; j++) {
12             dp[0] = dp[0] && s3[j - 1] == s2[j - 1];
13             for (int i = 1; i <= m; i++)
14                 dp[i] = (dp[i - 1] && s3[i + j - 1] == s1[i - 1]) || (dp[i] && s3[i + j - 1] == s2[j - 1]);
15         }
16         return dp[m];
17     }
18 };

 

目录
相关文章
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
71 0
|
7月前
|
机器学习/深度学习 canal NoSQL
从C语言到C++_12(string相关OJ题)(leetcode力扣)
从C语言到C++_12(string相关OJ题)(leetcode力扣)
56 0
|
7月前
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
55 0
|
算法 索引
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
LeetCode 438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.
93 0
LeetCode 438. Find All Anagrams in a String
|
存储
LeetCode 394. Decode String
给定一个经过编码的字符串,返回它解码后的字符串。 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。 你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。 此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
100 0
LeetCode 394. Decode String
|
索引
LeetCode 387. First Unique Character in a String
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
112 0
LeetCode 387. First Unique Character in a String
|
索引
LeetCode 345. Reverse Vowels of a String
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
105 0
LeetCode 345. Reverse Vowels of a String
|
机器学习/深度学习 NoSQL
LeetCode 344. Reverse String
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
100 0
LeetCode 344. Reverse String