-
Notifications
You must be signed in to change notification settings - Fork 0
/
42 Scrambled String Memoized.cpp
53 lines (33 loc) · 1.29 KB
/
42 Scrambled String Memoized.cpp
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
// 42). Scrambled String Memoized
// Video Link : https://youtu.be/VyHEglhbm-A
// LeetCode problem link : https://leetcode.com/problems/scramble-string/submissions/
// ------->>>>>>>>>>>------------>>>>>>>>>>>>________CODE________<<<<<<<<<<<<------------<<<<<<<<<<<<<<<<<<<<<<<<<<< //
class Solution {
public:
unordered_map<string,int> m;
bool isScramble(string s1, string s2) {
string key = s1 + " " + s2;
// precheck the key ,whether it's available in map or not
if (m.find(key) != m.end())
return m[key];
if (s1.compare(s2) == 0) {
return m[key] = true;
}
if (s1.length() <= 1) {
// equal to is used also becasue if it was single character and was same with s2 then it
// would alrady has been returned true by above if statement
m[key] = false;
return false;
}
int n = s1.length();
int flag = false;
for (int i = 1; i <= n - 1; i++) {
if ((isScramble(s1.substr(0, i), s2.substr(n - i, i)) && isScramble(s1.substr(i), s2.substr(0, n - i))) ||
(isScramble(s1.substr(0, i), s2.substr(0, i)) && isScramble(s1.substr(i), s2.substr(i)))) {
flag = true;
break;
}
}
return m[key] = flag;
}
};