Ctrl K

Shuffle Equal Chunks

Medium
You get two strings source and goal of the same length and an integer parts. The string source is cut into parts consecutive pieces of equal length. The pieces may then be put in any order and glued back together. Letters inside a piece cannot be moved. Return True if this can produce exactly goal, and False otherwise.
Example 1
source="xyzxyw", goal="xywxyz", parts=2 → True

Cut source into 2 pieces of 3 letters: "xyz" and "xyw". Swapping them gives "xywxyz".

Example 2
source="abab", goal="baab", parts=2 → False

The pieces are "ab" and "ab", and any order of them gives only "abab".

Example 3
source="pqr", goal="rqp", parts=3 → True
Constraints
  • 1 ≤ len(source) = len(goal) ≤ 2 · 10⁵
  • 1 ≤ parts ≤ len(source)
  • len(source) is divisible by parts
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAnagram Buckets MediumCharacters Grouped by Count MediumA Subsequence of One Word Only MediumMost Searched Words MediumWord Grown Letter by Letter Medium
loading Python…
Input
source="xyzxyw", goal="xywxyz", parts=2
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 45 tests on the server, hidden ones included.