Skip to content

Commit

Permalink
Time: 177 ms (37.61%), Space: 37.1 MB (33.16%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Mar 31, 2023
1 parent 84cc6d7 commit 4a42e22
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 0087-scramble-string/0087-scramble-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:

// declare an unordered map

unordered_map<string, bool> mp;

bool isScramble(string s1, string s2) {

// base case

if(s1 == s2)
return true;

// create an unique key

string key = s1 + "*" + s2;

// if already calculated

if(mp.count(key))
return mp[key];

for(int i = 0; i < s1.size() - 1; i++)
{
if(isScramble(s1.substr(0, i + 1), s2.substr(0, i + 1)) && isScramble(s1.substr(i + 1), s2.substr(i + 1)))
{
return mp[key] = true;
}

if(isScramble(s1.substr(0, i + 1), s2.substr(s2.size() - i - 1)) && isScramble(s1.substr(i + 1), s2.substr(0, s2.size() - i - 1)))
{
return mp[key] = true;
}
}

// store the res and return it

return mp[key] = false;
}
};

0 comments on commit 4a42e22

Please sign in to comment.