Skip to content

Commit

Permalink
Merge pull request #68 from abhishekyadav13/patch-1
Browse files Browse the repository at this point in the history
recursive solution
  • Loading branch information
avastino7 authored Oct 31, 2022
2 parents e298f87 + 00b5177 commit 01965cb
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions ScrambleString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
We can scramble a string s to get a string t using the following algorithm:
If the length of the string is 1, stop.
If the length of the string is > 1, do the following:
Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y.
Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.
Apply step 1 recursively on each of the two substrings x and y.
Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.
*/

class Solution {
public:
bool isScramble(string s1, string s2) {
if(s1==s2)
return true;

int len = s1.length();
int count[26] = {0};
for(int i=0; i<len; i++)
{
count[s1[i]-'a']++;
count[s2[i]-'a']--;
}

for(int i=0; i<26; i++)
{
if(count[i]!=0)
return false;
}

for(int i=1; i<=len-1; i++)
{
if( isScramble(s1.substr(0,i), s2.substr(0,i)) && isScramble(s1.substr(i), s2.substr(i)))
return true;
if( isScramble(s1.substr(0,i), s2.substr(len-i)) && isScramble(s1.substr(i), s2.substr(0,len-i)))
return true;
}
return false;
}
};

0 comments on commit 01965cb

Please sign in to comment.