316. Determine if Two Strings Are Close
Medium
Frequency Signature
Hashing
Problem
Two strings are considered close if one can be transformed into the other using any number of swaps between existing characters and swaps of the identities of two existing characters. Return true if word1 and word2 are close.
Examples
Example 1
Input: "abc", "bca"
Output: true
Example 2
Input: "a", "aa"
Output: false
Example 3
Input: "cabbba", "abbccc"
Output: true
Constraints
� 1 <= word1.length, word2.length <= 100000
� word1 and word2 contain lowercase English letters.
Hints
?? Both strings must contain exactly the same set of distinct characters.
?? The actual character identities of the frequencies may be exchanged.
?? Compare the sorted multisets of character frequencies.
Expected Complexity
Time: O(n)
Space: O(1)
Follow-up
Why is matching only the sorted frequency arrays insufficient without also checking the character sets?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
"abc", "bca"Expected
true