Skip to the code
cf.
+50 XP

#490 Shuffled Label Check

hard

A design tool can shuffle a text label with this recursive rule. A label of one letter stays as it is. A longer label is cut into two non-empty parts at any point; the two parts may be swapped or left in place; then each part is shuffled again with the same rule.

Write isShuffledLabel(original, shuffled) that returns true if shuffled can be produced from original by this rule, and false otherwise.

Example 1

Input: original = "panel", shuffled = "nelap"
Output: true

Cut panel into pa + nel and swap the parts, then cut pa into p + a and swap those.

Example 2

Input: original = "chart", shuffled = "rcath"
Output: false

No sequence of cuts and swaps produces it.

Constraints

  • 1 <= original.length <= 30
  • shuffled.length === original.length
  • Both contain only lowercase English letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).