Two strings are considered close if you can attain one from the other using the following operations:
abcde -> aecdbaacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)You can use the operations on either string as many times as necessary.
Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.
Example 1:
Input: word1 = "abc", word2 = "bca" Output: true Explanation: You can attain word2 from word1 in 2 operations. Apply Operation 1: "abc" -> "acb" Apply Operation 1: "acb" -> "bca"
Example 2:
Input: word1 = "a", word2 = "aa" Output: false Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
Example 3:
Input: word1 = "cabbba", word2 = "abbccc" Output: true Explanation: You can attain word2 from word1 in 3 operations. Apply Operation 1: "cabbba" -> "caabbb" Apply Operation 2: "caabbb" -> "baaccc" Apply Operation 2: "baaccc" -> "abbccc"
Constraints:
1 <= word1.length, word2.length <= 105word1 and word2 contain only lowercase English letters.Problem Overview: You are given two strings word1 and word2. Two strings are considered close if you can transform one into the other using two operations: swap any two characters, or transform every occurrence of one character into another existing character and vice versa. The task is to determine if such transformations can make the strings identical.
Approach 1: Compare Character Sets and Frequency (O(n + k log k) time, O(k) space)
The key observation is that transformations allow characters to be rearranged freely and also allow swapping frequency distributions between characters. Because of this, two conditions must hold. First, both strings must contain the exact same set of characters. If a character exists in one string but not the other, no operation can introduce it. Second, the frequency distributions of characters must match when sorted. Build frequency maps using a hash table, compare the character sets, then sort the frequency lists and check equality. Sorting the counts ensures that frequency patterns match even if the characters themselves differ.
Approach 2: Character Frequency Hashing (O(n + k) time, O(k) space)
This approach avoids sorting and instead relies on counting frequencies efficiently. Use two arrays or hash maps to count occurrences of each character while iterating through the strings once. Verify the character presence condition first by checking that both strings share the same character set. Then compare the multiset of frequency counts by hashing the counts themselves (for example using another map of frequency-of-frequency). Because the alphabet size k is small (26 lowercase letters), this runs in linear time relative to the string length. This technique relies heavily on counting and efficient lookups with a hash table.
Recommended for interviews: Interviewers typically expect the frequency-count insight. Start by confirming both strings share the same unique characters, then compare their frequency distributions. The sorted-frequency method is easy to reason about and widely accepted. The hashing approach demonstrates stronger understanding of string processing and counting patterns while maintaining linear time complexity.
To check whether two strings are close, you must determine if they can be made identical using the allowed operations. These operations permit swapping characters or changing all instances of one character to another, as long as they are existing characters in the particular string. Therefore, for two strings to be close:
Thus, comparing the character sets and their frequency distributions will be sufficient to determine if the strings are close.
This solution first checks if the strings have the same length, as differing lengths would make it impossible for them to be close. It then uses Python's Counter to count the occurrences of each character in both strings. The first condition checks if both strings have the same set of unique characters using the keys() method, which returns a set of unique characters. The second condition checks if the frequency distributions are identical by sorting and comparing the counted values.
Python
JavaScript
The time complexity is O(n + m), where n and m are the lengths of the strings, due to counting characters and comparing sets. Sorting the frequencies has a time complexity of O(k log k), where k is the number of unique characters. The space complexity is O(1) since we only store counts for at most 26 characters.
Alternative to directly sorting the character frequencies to compare them, we can map frequencies to counts using an intermediate structure, effectively hashing the frequency occurrences. This approach ensures that both overall character sets and their frequency distributions align.
Here, we first count the characters like before and then extract the keys into sets and compare them for equality. We use a HashMap to count the frequency of frequencies themselves, comparing these two frequency maps. Equal keys signify a valid transformation is possible between the two frequency distributions.
The time complexity remains O(n + m) due to creating and comparing maps, with a space complexity of O(1) due to counting up to 26 character frequencies.
According to the problem description, two strings are close if they meet the following two conditions simultaneously:
word1 and word2 must contain the same types of letters.word1 and word2 must be the same.Therefore, we can first use an array or hash table to count the occurrences of each letter in word1 and word2 respectively, and then compare whether they are the same. If they are not the same, return false early.
Otherwise, we sort the corresponding counts, and then compare whether the counts at the corresponding positions are the same. If they are not the same, return false.
At the end of the traversal, return true.
The time complexity is O(m + n + C times log C), and the space complexity is O(C). Here, m and n are the lengths of the strings word1 and word2 respectively, and C is the number of letter types. In this problem, C=26.
| Approach | Complexity |
|---|---|
| Compare Character Sets and Frequency | The time complexity is O(n + m), where n and m are the lengths of the strings, due to counting characters and comparing sets. Sorting the frequencies has a time complexity of O(k log k), where k is the number of unique characters. The space complexity is O(1) since we only store counts for at most 26 characters. |
| Character Frequency Hashing | The time complexity remains O(n + m) due to creating and comparing maps, with a space complexity of O(1) due to counting up to 26 character frequencies. |
| Counting + Sorting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Compare Character Sets and Sorted Frequencies | O(n + k log k) | O(k) | Simple and clear implementation when sorting frequency lists is acceptable |
| Character Frequency Hashing | O(n + k) | O(k) | Optimal approach when avoiding sorting and maintaining strict linear complexity |
Determine if Two Strings Are Close | Intuition | Google | Leetcode 1657 • codestorywithMIK • 19,272 views views
Watch 9 more video solutions →Practice Determine if Two Strings Are Close with our built-in code editor and test cases.
Practice on FleetCode