Advertisement

Scramble String - LeetCode 87 Solution

Scramble String - Complete Solution Guide

Scramble String is LeetCode problem 87, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 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 an

Detailed Explanation

The problem asks whether a string `s2` is a scrambled version of another string `s1`. A string is scrambled if it can be obtained from the original string through a series of operations involving splitting the string into two non-empty substrings and optionally swapping them, then recursively applying this process to the substrings. The input consists of two strings, `s1` and `s2`, of the same length, containing lowercase English letters. The output should be a boolean value: `true` if `s2` is a scrambled string of `s1`, and `false` otherwise. The strings' length is between 1 and 30, inclusive.

Solution Approach

The solution uses a recursive approach with memoization to determine if a string `s2` is a scrambled version of `s1`. The recursion breaks down the problem into smaller subproblems, checking if substrings of `s1` and `s2` are scrambled versions of each other. The function first checks if the substrings are identical or if their character counts differ; if so, it returns immediately. If the basic checks pass, it iterates through all possible split points `k` and recursively checks if the substrings can be scrambled with and without swapping.

Step-by-Step Algorithm

  1. Step 1: Check if the result for the current `(i1, i2, length)` combination is already stored in the `memo`. If yes, return the stored value.
  2. Step 2: Extract the substrings `sub1` from `s1` starting at `i1` with `length` and `sub2` from `s2` starting at `i2` with `length`.
  3. Step 3: If `sub1` and `sub2` are equal, store `true` in the `memo` for the key `(i1, i2, length)` and return `true`.
  4. Step 4: If the character frequencies of `sub1` and `sub2` are not equal, store `false` in the `memo` and return `false`.
  5. Step 5: Iterate through all possible split points `k` from 1 to `length - 1`.
  6. Step 6: For each `k`, recursively check two scenarios:
  7. a. No Swap: Check if `s1[i1:i1+k]` is scrambled to `s2[i2:i2+k]` AND `s1[i1+k:i1+length]` is scrambled to `s2[i2+k:i2+length]`
  8. b. Swap: Check if `s1[i1:i1+k]` is scrambled to `s2[i2+length-k:i2+length]` AND `s1[i1+k:i1+length]` is scrambled to `s2[i2:i2+length-k]`
  9. Step 7: If either of the above scenarios returns `true`, store `true` in the `memo` and return `true`.
  10. Step 8: If all the split points are checked and neither scenario returns `true`, store `false` in the `memo` and return `false`.

Key Insights

  • Insight 1: The core idea is to use recursion with memoization. A string `s1` can be scrambled into `s2` if either `s1` and `s2` are identical, or there exists an index `k` where the substrings `s1[0:k]` is scrambled into `s2[0:k]` and `s1[k:]` is scrambled into `s2[k:]`, or `s1[0:k]` is scrambled into `s2[n-k:]` and `s1[k:]` is scrambled into `s2[0:n-k]` where n is the length of the strings.
  • Insight 2: Using memoization drastically reduces the computational overhead by storing the results of previously computed subproblems.
  • Insight 3: Before recursion, checking if the character counts of the substrings are equal provides a quick way to prune unnecessary recursive calls. If the character counts are different, the strings cannot be scrambled versions of each other.

Complexity Analysis

Time Complexity: O(n^5)

Space Complexity: O(n^3)

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: Amazon, Darwinbox, Google, Media.net, Rubrik.