leetcode #97 in cpp
2016-06-15 13:20
309 查看
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 =
s2 =
When s3 =
When s3 =
Solution:
A simple idea is to keep 3 pointers p1, p2, p3, each on s1, s2, and s3. We could only move pointers p1 or p2 forward if current character p1 or p2 points to matches the character p3 points to. When p1 and p2 point characters both of which do not match
p3's character, we move back p1 and p2 back to last match location and try a different move.
Say s1 = "ab", s2 = "ac", and s3 = "abac".
[a(p1),b]
[a(p2), c]
[a(p3),b,a,c].
both p1 and p2 matches p3, we move p3. We can move p1 or p2. we choose to move p2.
[a(p1),b]
[a,c(p2)]
[a,b(p3),a,c]
neither p1 nor p2 matches p3. We step back to last match and try a different move.
[a(p1),b]
[a(p2), c]
[a(p3),b,a,c].
We try to move p1 instead of p2 which we have tried.
[a,b(p1)]
[a(p2), c]
[a,b(p3),a,c].
p1 matches p3. move p1 and p3.
[a,b](p1)
[a(p2), c]
[a,b,a(p3),c].
Only p2 is active now since p1 is out of bound which means s1 is used up. p2 matches p3. move p2 and p3.
[a,b](p1)
[a, c(p2)]
[a,b,a,c(p3)].
p2 matches p3. We are done.
This is a simple idea. The problem is where should we go back and try different move when we are at a place neither p1 nor p2 do not meet p3.
We could use recursion to solve this problem. We wait for the feedback of the move we try. If it fails then we try next different move.
Code: (This recursion works but exceeds time limit)
The recursion exceeds the time limit. But it gives us the idea how to approach this problem.
In the code we could get a sense that s1[0....ind1] and s2[0....ind2] matches s3[0....ind3] depending on s1[ind1], s2[ind2], and if s1[0.....?] and s2[0....?] matches s3[0....ind3].
This looks like a DP problem.
Suppose we have a DP array dp[s1.length()+1][s2.length()+1], where dp[i][j] = true/false if s1[0...i-1] and s2[0....j-1] could/could not form s3[0....i+j-1].
dp[i][j] = true only if either of the following is true:
1. dp[i-1][j] = true && s1[i-1] == s3[i+j-1]. (s1[0...i-2] and s2[0...j-1] forms s3[0....i+j-2], and s1[i-1] forms s3[i+j-1])
2. dp[i][j-1] = true && s2[j-1] == s3[i+j-1]. (s1[0...i-1] and s2[0...j-2] forms s3[0....i+j-2], and s2[j-1] forms s3[i+j-1])
Code:
For example,
Given:
s1 =
"aabcc",
s2 =
"dbbca",
When s3 =
"aadbbcbcac", return true.
When s3 =
"aadbbbaccc", return false.
Solution:
A simple idea is to keep 3 pointers p1, p2, p3, each on s1, s2, and s3. We could only move pointers p1 or p2 forward if current character p1 or p2 points to matches the character p3 points to. When p1 and p2 point characters both of which do not match
p3's character, we move back p1 and p2 back to last match location and try a different move.
Say s1 = "ab", s2 = "ac", and s3 = "abac".
[a(p1),b]
[a(p2), c]
[a(p3),b,a,c].
both p1 and p2 matches p3, we move p3. We can move p1 or p2. we choose to move p2.
[a(p1),b]
[a,c(p2)]
[a,b(p3),a,c]
neither p1 nor p2 matches p3. We step back to last match and try a different move.
[a(p1),b]
[a(p2), c]
[a(p3),b,a,c].
We try to move p1 instead of p2 which we have tried.
[a,b(p1)]
[a(p2), c]
[a,b(p3),a,c].
p1 matches p3. move p1 and p3.
[a,b](p1)
[a(p2), c]
[a,b,a(p3),c].
Only p2 is active now since p1 is out of bound which means s1 is used up. p2 matches p3. move p2 and p3.
[a,b](p1)
[a, c(p2)]
[a,b,a,c(p3)].
p2 matches p3. We are done.
This is a simple idea. The problem is where should we go back and try different move when we are at a place neither p1 nor p2 do not meet p3.
We could use recursion to solve this problem. We wait for the feedback of the move we try. If it fails then we try next different move.
Code: (This recursion works but exceeds time limit)
bool check(string &s1, string &s2, string &s3, int ind1, int ind2, int ind3){ if(ind1 == s1.length() && ind2 == s2.length() && ind3 == s3.length()){ return true; } if(ind3 >= s3.length() || (ind1 >= s1.length() && ind2 >= s2.length()) ) return false; bool res = false; if(ind1<s1.length() && ind2 < s2.length() && s1[ind1]!=s3[ind3] && s2[ind2] != s3[ind3]) return false; if(ind1 < s1.length()){ if(s1[ind1] == s3[ind3]){ if(check(s1,s2,s3, ind1+1, ind2, ind3+1)) return true; } else if(ind2 >= s2.length()) return false; } if(ind2 < s2.length()){ if(s2[ind2] == s3[ind3]){ if(check(s1,s2,s3,ind1, ind2+1, ind3+1)) return true; }else if(ind1>=s1.length()) return false; } return false; }
The recursion exceeds the time limit. But it gives us the idea how to approach this problem.
In the code we could get a sense that s1[0....ind1] and s2[0....ind2] matches s3[0....ind3] depending on s1[ind1], s2[ind2], and if s1[0.....?] and s2[0....?] matches s3[0....ind3].
This looks like a DP problem.
Suppose we have a DP array dp[s1.length()+1][s2.length()+1], where dp[i][j] = true/false if s1[0...i-1] and s2[0....j-1] could/could not form s3[0....i+j-1].
dp[i][j] = true only if either of the following is true:
1. dp[i-1][j] = true && s1[i-1] == s3[i+j-1]. (s1[0...i-2] and s2[0...j-1] forms s3[0....i+j-2], and s1[i-1] forms s3[i+j-1])
2. dp[i][j-1] = true && s2[j-1] == s3[i+j-1]. (s1[0...i-1] and s2[0...j-2] forms s3[0....i+j-2], and s2[j-1] forms s3[i+j-1])
Code:
bool isInterleave(string s1, string s2, string s3) { int len1 = s1.length(); int len2 = s2.length(); int len3 = s3.length(); if(len1+len2!=len3) return false; if(s1[0] != s3[0] && s2[0] != s3[0]) return false; vector<vector<bool>> dp(len1+1, vector<bool>(len2+1,false)); dp[0][0] = true; for(int i = 1; i <= len1; i ++){ dp[i][0] = s1.substr(0, i) == s3.substr(0,i)?true:false; } for(int i = 1; i <= len2; i ++){ dp[0][i] = s2.substr(0, i) == s3.substr(0,i)?true:false; } //dp[i][j] = true if s1[0...i-1] s2[0...j-1] matches s3[i+j-1] for(int i = 1; i <= len1; i++){ for(int j = 1; j <= len2; j ++){ if(dp[i-1][j]){ dp[i][j] = dp[i][j] || s1[i-1] == s3[i+j-1]; } if(dp[i][j-1]){ dp[i][j] =dp[i][j] || s2[j-1] == s3[i+j-1]; } } } return dp[len1][len2]; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解