Recognize Recursive Split-and-Swap String Transformations

Quick Overview

Decide whether recursive nonempty splits and optional part swaps transform one equal-length string into another.

Recognize Recursive Split-and-Swap String Transformations

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem A string can be scrambled recursively as follows. A one-character string stays as it is. For a longer string, choose a split into two nonempty contiguous parts, optionally exchange the two parts, and apply the same process recursively within each part. Given equal-length strings `s1` and `s2`, return whether some sequence of these choices transforms `s1` into `s2`. This asks whether a transformation exists, not how likely random choices are to produce it. ```cpp bool isScramble(string s1, string s2); ``` ### Examples ```text Input: s1 = "abcd", s2 = "badc" Output: true ``` Split into `ab` and `cd`, keep the two parts in order, and exchange the letters inside both parts. ```text Input: s1 = "abcd", s2 = "bdac" Output: false ``` These strings have the same letter counts, but no allowed recursive splitting produces the second. ### Constraints - `s1.length == s2.length`. - The common length is between 1 and 30. - Both strings contain only lowercase English letters.

Overview: Decide whether recursive nonempty splits and optional part swaps transform one equal-length string into another.

Google logo
Google
Date not reported
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A string can be scrambled recursively as follows. A one-character string stays as it is. For a longer string, choose a split into two nonempty contiguous parts, optionally exchange the two parts, and apply the same process recursively within each part.

Given equal-length strings s1 and s2, return whether some sequence of these choices transforms s1 into s2. This asks whether a transformation exists, not how likely random choices are to produce it.

bool isScramble(string s1, string s2);

Examples

Input: s1 = "abcd", s2 = "badc"
Output: true

Split into ab and cd, keep the two parts in order, and exchange the letters inside both parts.

Input: s1 = "abcd", s2 = "bdac"
Output: false

These strings have the same letter counts, but no allowed recursive splitting produces the second.

Constraints

  • s1.length == s2.length .
  • The common length is between 1 and 30.
  • Both strings contain only lowercase English letters.