-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from abhishekyadav13/patch-1
recursive solution
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |