The two operations decide everything about this problem, so the first step is working out what each one allows and what it can never change.
Operation 1 (swap any two characters) lets us rearrange a string into any order, so two anagrams can always be transformed into each other with Operation 1 alone. Operation 2 swaps the identities of two existing characters: every a becomes a b and every b becomes an a, simultaneously. Described in terms of counts, Operation 2 swaps the frequencies of two characters that both appear in the string.
Free rearrangement makes character positions irrelevant. Identity swaps make the assignment of letters to counts irrelevant. Two properties survive both operations: the set of characters present (Operation 2 maps existing characters to existing characters, so a new letter can never appear) and the multiset of frequency counts.
Two strings are close if and only if they have the same length, the same set of distinct characters, and the same sorted list of character frequencies.
1 <= word1.length, word2.length <= 10^5 → With n up to 100,000, we need O(n) or O(n log n). An O(n^2) approach would be too slow.word1 and word2 contain only lowercase English letters → At most 26 distinct characters. This means frequency arrays of size 26 are constant space, and sorting 26 elements is O(1).A brute force models the operations directly. Operation 1 already handles rearrangement, so positions can be ignored and the question becomes one about frequencies: can some sequence of Operation 2 identity swaps make word1's frequency distribution equal to word2's? Each Operation 2 swap exchanges the counts of two existing characters, and any permutation can be composed from pairwise swaps, so repeated applications can redistribute word1's frequency values among its own characters in every possible arrangement.
The brute force enumerates those arrangements: for each permutation of word1's frequency values over its character set, check whether the resulting distribution equals word2's. Only characters that appear in word1 need to participate, but with up to 26 distinct letters the worst case is still 26! permutations, far too many to enumerate. The approach is useful here because it makes explicit what the optimal solution checks in one step.
word1 and word2 differ, return false.word1 and their frequency values.word1's characters.word2's frequency array exactly, return true.Loading animation...
The permutation search is also unnecessary. A matching assignment exists exactly when the two multisets of frequency values are equal, and equality of multisets can be tested directly by sorting both and comparing. That observation replaces the factorial search with a constant-size sort.
Neither operation can change two properties of a string. Operation 1 only reorders characters, and Operation 2 only exchanges the counts of two characters that already appear, so the set of distinct characters and the multiset of frequency values stay fixed no matter how many operations run. If two strings differ in either property, they cannot be close.
Matching on both properties is also sufficient. Given the same character set and the same frequency multiset, apply Operation 2 swaps to give each character of word1 the count it has in word2 (any permutation of counts can be composed from pairwise swaps), then apply Operation 1 swaps to rearrange the result into word2 exactly.
Comparing the frequency multisets does not require trying permutations: sort both frequency arrays and check equality. Two multisets are equal exactly when their sorted forms are identical. The character-set condition still needs its own check, because sorting destroys the information about which character held which count. word1 = "uau" and word2 = "ssx" have the same sorted frequencies [1, 2] but different character sets, and no operation can turn a u into an s.
So the check is: same length, same set of distinct characters, identical sorted frequency arrays.
word1 and word2 have different lengths, return false.Loading animation...