Scramble String LeetCode
In this problem, we must determine whether one string can be transformed into another by performing a series of character splits and swaps. This concept has practical applications in cryptography, where strings are often manipulated through various operations to achieve encryption or decryption. Additionally, it is relevant in data compression algorithms that rearrange or transform strings to optimize space usage. Imagine we’re playing a word game where you have a set of letters, and our goal is to create as many valid words as possible with those letters. This is similar to the Scramble String problem.
In the illustration above, we can see that great can be scrambled to rgaet. Here, we’ll explore the problem statement and provide a Python solution and explanation.
Problem statement
Given two strings, s1 and s2 of the same length, return TRUE if s2 is a scrambled string of s1; otherwise, return FALSE.
To get one string from another, we need to follow the steps below:
If the string’s length is
1, no further action is taken.If the string’s length is greater than
1, perform the following:Divide the string into two substrings (nonempty) at a random index. In other words, if the string is
s, we can split it into two parts:xandy, such thats = x + y.Either swap the positions of the substrings or keep them in their original order (the decision is taken randomly). This means
smay becomes = x + yors = y + x.Apply the scrambling process recursively to the two substrings,
xandy. Given two strings,s1, ands2, which have the same length; the goal is to determine whethers2can be derived from applying the scramble operation ons1. If this is possible, returnTrue; otherwise, returnFalse.
Knowledge test
Attempt the quiz below to test your concepts on this problem:
What is the Scramble String problem in Python?
Rearranging characters in a string to form a palindrome
Splitting a string into two substrings and swapping them to match another string
Finding the longest common subsequence between two strings
A problem that involves reversing a string
Algorithm
We use a dynamic programming approach with a 3D table to store intermediate results efficiently, avoiding redundant computations and facilitating memoization, thus improving the algorithm’s efficiency.
The algorithm can be described as follows:
Base case:
If the lengths of
s1ands2are not equal, return FALSE as the transformation is not possible.
Table initialization:
Create a 3D table
tablewith dimensions[n][n][n + 1]wherenis the length of the stringss1ands2. Initialize all values to FALSE.
Base case for single characters:
Iterate over characters in both strings. If characters in the same positions are equal, set
table[i][j][1]to TRUE, indicating that a single-character substring ofs1can be scrambled to match that ofs2.
Dynamic programming:
Use nested loops to fill values in the table. Iterate over the lengths of the substrings, starting positions, and splitting positions.
For each substring length from 2 to
n, for each starting positioniins1, and for each starting positionjins2, check if it’s possible to split, scramble, and match the current substrings ofs1ands2.Set
table[i][j][length]toTrueif the scramble is possible by checking the possible splits and their combinations..
Result:
The final result is stored in
table[0][0][len(s1)], indicating whether the entire stringss1ands2can be scrambled to match each other.
This can be seen in the slides below:
Educative 99 helps you solve complex coding problems, such as the Scramble String problem, by recognizing patterns and applying the right algorithms.
Code
The code for this problem is as follows:
def isScramble(s1, s2):if len(s1) != len(s2):return Falsen = len(s1)table = [[[False] * (n + 1) for _ in range(n)] for _ in range(n)]# Base case for single charactersfor i in range(n):for j in range(n):if s1[i] == s2[j]:table[i][j][1] = True# Dynamic programming to fill the tablefor length in range(2, n + 1):for i in range(n - length + 1):for j in range(n - length + 1):for k in range(1, length):if (table[i][j][k] and table[i + k][j + k][length - k]) or (table[i][j + length - k][k] and table[i + k][j][length - k]):table[i][j][length] = Truebreakreturn table[0][0][len(s1)]# Driver Codedef main():# Test casestest_cases = [("great", "rgeat", True),("abcde", "caebd", False),("a", "a", True),("abc", "bca", True),("abcd", "bdac", False)]for s1, s2, expected in test_cases:result = isScramble(s1, s2)print(f"isScramble({s1}, {s2}) = {result} (expected: {expected})")if __name__ == "__main__":main()
Time complexity
The time complexity of the isScramble function is s1 up tos2 up to
Space complexity
The space complexity of the isScramble function is n is the length of the input strings s1 and s2. This is due to the size of the 3D table.
Free Resources