-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_recursion_memoization_2d.cpp
More file actions
59 lines (47 loc) · 1.74 KB
/
code_recursion_memoization_2d.cpp
File metadata and controls
59 lines (47 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// This take the code of 3D and reduces 1 dimensions
// Because p3 is just p1+p2 at all times
class Solution {
public:
vector<vector<bool>> dp;
vector<vector<bool>> visited;
bool checkInterleaving(int p1, int p2, string s1, string s2, string s3) {
// Check if the positions is still under the radar
if (p1 == s1.length() && p2 == s2.length() && p1+p2 == s3.length()) {
return true;
}
else if (p1 == s1.length() && p2 == s2.length() && p1+p2 < s3.length()) {
// We have reached the end of s1 and s2 but there is still some left in s3
return false;
}
else if (p1+p2 == s3.length() && (p1<s1.length() || p2<s2.length())) {
// We have reached the end of s3, but we still have some characters left over in s1 or s2
return false;
}
if (visited[p1][p2]) {
return dp[p1][p2];
}
bool isPossible = false;
if (p1<s1.length() && p2<s2.length() && s3[p1+p2] == s1[p1] && s3[p1+p2] == s2[p2]) {
// Character matches from both s1 and s2, so find choices
// Take it from s1
isPossible = isPossible || checkInterleaving(p1+1, p2, s1, s2, s3);
// Take it from s2
isPossible = isPossible || checkInterleaving(p1, p2+1, s1, s2, s3);
}
else if (p1<s1.length() && s3[p1+p2] == s1[p1]) {
isPossible = checkInterleaving(p1+1, p2, s1, s2, s3);
}
else if (p2<s2.length() && s3[p1+p2] == s2[p2]) {
isPossible = checkInterleaving(p1, p2+1, s1, s2, s3);
}
dp[p1][p2] = isPossible;
visited[p1][p2] = true;
return isPossible;
}
bool isInterleave(string s1, string s2, string s3) {
// Create a store for storing repeated computations
dp.resize(s1.length()+1, vector<bool>(s2.length()+1));
visited.resize(s1.length()+1, vector<bool>(s2.length()+1));
return checkInterleaving(0, 0, s1, s2, s3);
}
};