In the evening, I made an interleaving string of leetcode. I think it is especially suitable for comparing the efficiency of tree traversal and dynamic planning.
The questions are as follows:
GivenS1,S2,S3, Find whetherS3Is formed by the interleavingS1AndS2.
For example,
Given:
S1="aabcc",
S2="dbbca",
WhenS3="aadbbcbcac", Return true.
WhenS3="aadbbbaccc", Return false.
The first response is suitable for recursion:
bool isInterleave_res(string s1, string s2, string s3) { if ((s1.empty() && s2.empty() && s3.empty()) || (!s1.empty() && !s3.empty() && s3[0] == s1[0] && isInterleave_res(s1.substr(1, s1.length()-1), s2, s3.substr(1, s3.length()-1))) || (!s2.empty() && !s3.empty() && s3[0] == s2[0] && isInterleave_res(s1, s2.substr(1, s2.length()-1), s3.substr(1, s3.length()-1))) ) { return true; } return false; }
In this way, the depth of the tree is first traversed, but the efficiency is relatively low. Many nodes will be computed multiple times. Indeed, TLE, the use case is:
S1 = "success ";
S2 = "bytes ";
S3 = "maid ";
It is easy to use dynamic planning to set the table DP, DP [I] [J] indicates that the first I of S1 and the first J of S2 can match the first I + J of S3, based on DP [I] [J], you can launch DP [I + 1] [J] and DP [I] [J + 1]. Returns the value at the bottom right of DP.
Example 1:S3="aadbbcbcac"
Example 2:S3="aadbbcbccc"
class Solution {public: bool isInterleave(string s1, string s2, string s3) { int len1 = s1.length(), len2 = s2.length(), len3=s3.length(); if (len1+len2!=len3) { return false; } vector<vector<bool>> dp(len1+1, vector<bool>(len2+1, false)); dp[0][0] = true; for (int i = -1; i < len1; ++i) { for (int j = -1; j < len2; ++j) { if (dp[i+1][j+1]==true) { if (i+1 < len1 && i+j+2 < len3 && s1[i+1]==s3[i+j+2]) { dp[i+2][j+1] = true; } if (j+1 < len2 && i+j+2 < len3 && s2[j+1]==s3[i+j+2]) { dp[i+1][j+2] = true; } } } } return dp[len1][len2]; }};
In this way, the Code passes.
After careful observation, traversal is a binary tree's depth-first traversal. The DP table corresponding to this tree is the left node of (I, j) (I + 1, J) the right node is (I, j + 1), so the right node of (I + 1, J) and (I, j + 1) the left node of is the same (I + 1, J + 1), further intensifying the repetition of the traversal tree.
However, observing the DP table also wastes a lot. If the S1 length is m and the S2 length is n, the time complexity and space complexity of DP are both M * n (the space complexity can be optimized to 2 x m or 2 x N, because it is only related to the previous row ), but in fact, we only care about the point T. In the best case, we only need m + n.
We can combine the tree traversal (only the point T is taken) with the DP table to save the DP table in the form of a dilution matrix and only the point T is saved, the T-points that we walk through will not be traversed. In this way, M and N should be greatly improved. The Code is as follows:
class Solution {public: bool isInterleave(string s1, string s2, string s3) { int len1 = s1.length(), len2 = s2.length(), len3=s3.length(); if (len1+len2!=len3) { return false; } stack<pair<int, int>> node; set<pair<int, int>> passed; node.push(pair<int, int>(-1, -1)); pair<int, int> last_node; int count = 0; while (!node.empty()) { count++; int i = node.top().first; int j = node.top().second; passed.insert(pair<int, int>(i, j)); if (i == len1-1 && j == len2-1) { return true; } node.pop(); if (i+1 < len1 && i+j+2 < len3 && s1[i+1]==s3[i+j+2] && passed.find(pair<int, int>(i+1, j)) == passed.end()) { node.push(pair<int, int>(i+1, j)); } if (j+1 < len2 && i+j+2 < len3 && s2[j+1]==s3[i+j+2] && passed.find(pair<int, int>(i, j+1)) == passed.end()) { node.push(pair<int, int>(i, j+1)); } } return false; }};
Three methods: Time limit exceeded, 16 ms for all test cases, and 8 ms for the third
[Leetcode] interleaving string for traversal and Dynamic Planning