Last updated on August 3rd, 2024 at 10:15 pm
Here, We see Scramble String LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.
List of all LeetCode Solution
Topics
String, Dynamic Programming
Level of Question
Hard
Scramble String LeetCode Solution
Table of Contents
Problem Statement
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 tox
andy
wheres = x + y
. - Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step,
s
may becomes = x + y
ors = y + x
. - Apply step 1 recursively on each of the two substrings
x
andy
.
- Split the string into two non-empty substrings at a random index, i.e., if the string is
Given two strings s1
and s2
of the same length, return true
if s2
is a scrambled string of s1
, otherwise, return false
.
Example 1: Input: s1 = "great", s2 = "rgeat" Output: true Explanation: One possible scenario applied on s1 is: "great" --> "gr/eat" // divide at random index. "gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order. "gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at random index each of them. "g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order. "r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t". "r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order. The algorithm stops now, and the result string is "rgeat" which is s2. As one possible scenario led s1 to be scrambled to s2, we return true. Example 2: Input: s1 = "abcde", s2 = "caebd" Output: false Example 3: Input: s1 = "a", s2 = "a" Output: true
1. Scramble String Leetcode Solution C++
class Solution { public: unordered_map<string,bool> mp; //Create Map Globally bool isScramble(string s1, string s2) { if(s1.size() != s2.size()) //Checking length of both string return false; if(s1.size()==0 && s2.size()==0) //If both strings are empty return true; if(s1.compare(s2)==0) //If both strings are same return true; if(s1.size()<=1) //Check size of String s1 is less tha 1 OR not return false; string key=s1; //Generating Key for Map which should be unique key.push_back(' '); key.append(s2); if(mp.find(key) != mp.end()) //Storing value in map if found return mp[key]; /*Till now if we didn't find our value then we will calculate particularly */ int n=s1.size(); bool flag=false; for(int i=1;i<n;i++) { if((isScramble(s1.substr(0,i),s2.substr(n-i,i))==true && isScramble(s1.substr(i,n-i),s2.substr(0,n-i))==true) || (isScramble(s1.substr(0,i),s2.substr(0,i))==true && isScramble(s1.substr(i,n-i),s2.substr(i,n-i))==true)) { flag=true; break; } } return mp[key]=flag; } };
2. Scramble String Leetcode Solution Java
class Solution { public boolean isScramble(String s1, String s2) { if (s1.length() != s2.length()) return false; int len = s1.length(); boolean [][][] F = new boolean[len][len][len + 1]; for (int k = 1; k <= len; ++k) for (int i = 0; i + k <= len; ++i) for (int j = 0; j + k <= len; ++j) if (k == 1) F[i][j][k] = s1.charAt(i) == s2.charAt(j); else for (int q = 1; q < k && !F[i][j][k]; ++q) { F[i][j][k] = (F[i][j][q] && F[i + q][j + q][k - q]) || (F[i][j + k - q][q] && F[i + q][j][k - q]); } return F[0][0][len]; } }
3. Scramble String Leetcode Solution JavaScript
var isScramble = function(s1, s2) { return checkScramble(s1, 0, s1.length, s2, 0, s2.length) } function checkScramble(string1, i1, j1, string2, i2, j2, memory = {}) { const n = j1 - i1 const key = 1e9 * i1 + 1e6 * j1 + 1e3 * i2 + 1e0 * j2 if (key in memory) { return memory[key] } let codesum = 0 for (let i = 0; i < n; i++) { codesum += string1.charCodeAt(i1 + i) ** 2 - string2.charCodeAt(i2 + i) ** 2 } if (codesum !== 0) { return memory[key] = false } if (n === 1) { return memory[key] = true } for (let i = 1; i < n; i++) { if ( checkScramble(string1, i1, i1 + i, string2, i2, i2 + i, memory) && checkScramble(string1, i1 + i, j1, string2, i2 + i, j2, memory) ) { return memory[key] = true } if ( checkScramble(string1, i1, i1 + i, string2, j2 - i, j2, memory) && checkScramble(string1, i1 + i, j1, string2, i2, j2 - i, memory) ) { return memory[key] = true } } return memory[key] = false };
4. Scramble String Leetcode Solution Python
class Solution(object): def __init__(self): self.dic = {} def isScramble(self, s1, s2): if (s1, s2) in self.dic: return self.dic[(s1, s2)] if len(s1) != len(s2) or sorted(s1) != sorted(s2): # prunning self.dic[(s1, s2)] = False return False if s1 == s2: self.dic[(s1, s2)] = True return True for i in xrange(1, len(s1)): if (self.isScramble(s1[:i], s2[:i]) and self.isScramble(s1[i:], s2[i:])) or \ (self.isScramble(s1[:i], s2[-i:]) and self.isScramble(s1[i:], s2[:-i])): return True self.dic[(s1, s2)] = False return False