-
Notifications
You must be signed in to change notification settings - Fork 94
/
Scramble_string_problem.cpp
86 lines (60 loc) · 2.55 KB
/
Scramble_string_problem.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Solution {
public:
//for storing already solved problems
unordered_map<string,bool> mp;
bool isScramble(string s1, string s2) {
//base cases
int n = s1.size();
//if both string are not equal in size
if(s2.size()!=n)
return false;
//if both string are equal
if(s1==s2)
return true;
//if code is reached to this condition then following this are sure:
//1. size of both string is equal
//2. string are not equal
//so size is equal (where size==1) and they are not equal then obviously false
//example 'a' and 'b' size is equal ,string are not equal
if(n==1)
return false;
string key = s1+" "+s2;
//check if this problem has already been solved
if(mp.find(key)!=mp.end())
return mp[key];
//for every iteration it can two condition
//1.we should proceed without swapping
//2.we should swap before looking next
for(int i=1;i<n;i++)
{
//ex of without swap: gr|eat and rg|eat
bool withoutswap = (
//left part of first and second string
isScramble(s1.substr(0,i),s2.substr(0,i))
&&
//right part of first and second string;
isScramble(s1.substr(i),s2.substr(i))
);
//if without swap give us right answer then we do not need
//to call the recursion withswap
if(withoutswap)
return true;
//ex of withswap: gr|eat rge|at
//here we compare "gr" with "at" and "eat" with "rge"
bool withswap = (
//left part of first and right part of second
isScramble(s1.substr(0,i),s2.substr(n-i))
&&
//right part of first and left part of second
isScramble(s1.substr(i),s2.substr(0,n-i))
);
//if withswap give us right answer then we return true
//otherwise the for loop do it work
if(withswap)
return true;
//we are not returning false in else case
//because we want to check further cases with the for loop
}
return mp[key] = false;
}
};