Ctrl K

Shuffle and Relabel

Medium
You are given two words first and second made of lowercase English letters. Any number of times you may apply two operations to a word: - **swap** the positions of any two letters, e.g. abcde → aecdb; - **relabel** two letters present in the word: every occurrence of one becomes the other and vice versa, e.g. aacabb → bbcbaa (a and b traded places). Return True if one word can be turned into the other with these operations, and False otherwise.
Example 1
first='tree', second='rett' → True

Swap the names of t and e: "tree" turns into "ertt", and rearranging the letters gives "rett".

Example 2
first='aab', second='bbc' → False

The first word has no c, and neither operation can introduce a new letter.

Example 3
first='abbccc', second='aabbbc' → True
Constraints
  • 1 ≤ len(first), len(second) ≤ 10⁵
  • Both words contain only lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCharacters Grouped by Count MediumMost Searched Words MediumNo Twin Letters Side by Side MediumRanking Finalists by Jury Ballots MediumNine-Button Layout Medium
loading Python…
Input
first='tree', second='rett'
Expected
True

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