Problem
You get two strings, word1 and word2. The strings are close if you can change word1 into word2 with the two operations below. Return true if the strings are close, and false otherwise.
Operation 1: swap any two characters of the string. For example, this operation changes "cab" into "bac".
Operation 2: pick two different characters that are both in the string. Change every copy of the first character into the second, and every copy of the second into the first. For example, with x and y, "xxyz" becomes "yyxz".
You can use each operation any number of times.
Example 1
- Input:
- word1 = "lime", word2 = "mile"
- Output:
- true
- Explanation:
word2has the same characters asword1in a different order. Operation 1 changes the order.
Example 2
- Input:
- word1 = "xxyyy", word2 = "yyxxx"
- Output:
- true
- Explanation:
- Operation 2 with
xandychangesword1intoword2in one step.
Example 3
- Input:
- word1 = "seed", word2 = "deep"
- Output:
- false
- Explanation:
word2has the characterp, butword1does not. No operation adds a new character.
Constraints
- 1 <= word1.length, word2.length <= 10^5
- word1 and word2 have only lowercase English letters.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 1657. Determine if Two Strings Are Close
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source