Skip to content
Decoder

Determine if Two Strings Are Close

Medium

Problem

Two strings are close if you can turn one into the other using these moves **as many times as you like**:

1. Swap any two characters (positions do not matter): abc → cba. 2. Swap the roles of two letters that both exist: every a becomes b and every b becomes a. For example aacabb → bbcbaa.

Given word1 and word2, return true if they are close.

Example 1

Input
word1 = "abc", word2 = "bca"
Output
true

Example 2

Input
word1 = "a", word2 = "aa"
Output
false

Example 3

Input
word1 = "cabbba", word2 = "abbccc"
Output
true

Constraints

  • 1 <= word1.length, word2.length <= 10^5
  • Both strings contain only lowercase English letters.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.