Groups of Special-Equivalent Strings - Solution & Explanation
Problem Statement
You are given an array of strings of the same length words.
In one move, you can swap any two even indexed characters or any two odd indexed characters of a string words[i].
Two strings words[i] and words[j] are special-equivalent if after any number of moves, words[i] == words[j].
- For example,
words[i] = "zzxy"andwords[j] = "xyzz"are special-equivalent because we may make the moves"zzxy" -> "xzzy" -> "xyzz".
A group of special-equivalent strings from words is a non-empty subset of words such that:
- Every pair of strings in the group are special equivalent, and
- The group is the largest size possible (i.e., there is not a string
words[i]not in the group such thatwords[i]is special-equivalent to every string in the group).
Return the number of groups of special-equivalent strings from words.
Example 1:
Input: words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"] Output: 3 Explanation: One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings is all pairwise special equivalent to these. The other two groups are ["xyzz", "zzxy"] and ["zzyx"]. Note that in particular, "zzxy" is not special equivalent to "zzyx".
Example 2:
Input: words = ["abc","acb","bac","bca","cab","cba"] Output: 3
Constraints:
1 <= words.length <= 10001 <= words[i].length <= 20words[i]consist of lowercase English letters.- All the strings are of the same length.
Approach Overview
Problem Overview: You receive an array of strings. Two strings are special-equivalent if you can swap characters among even indices or among odd indices any number of times and transform one string into the other. The task is to count how many distinct groups of such equivalent strings exist.
Approach 1: Group by Sorted Characters (O(n * k log k) time, O(n * k) space)
The allowed swaps mean characters at even indices can rearrange among themselves, and characters at odd indices can rearrange among themselves. For each string, split characters into two buckets: even-indexed and odd-indexed. Sort both buckets independently and combine them into a canonical key such as sortedEven + '#' + sortedOdd. Insert this key into a hash set to track unique groups. If two strings produce the same key, they belong to the same special-equivalent group. This method relies on sorting to normalize both parity positions and uses a hash table or set for grouping.
Approach 2: Character Frequency Counting (O(n * k) time, O(n) space)
Sorting is not strictly necessary. Since characters only rearrange within parity groups, the exact order is irrelevant; only the counts matter. For each string, build two frequency arrays of size 26: one for even positions and one for odd positions. Concatenate the counts into a compact signature (for example, a tuple or string). Insert this signature into a hash set to represent the group. Two strings with identical even and odd frequency distributions must be special-equivalent. This avoids sorting entirely and reduces the complexity to linear time per string. The solution heavily uses array indexing and string processing.
Recommended for interviews: Character Frequency Counting is usually the expected solution. It demonstrates recognition that order does not matter within parity groups and reduces the cost from k log k sorting to linear counting. The sorting approach still works and is easier to implement quickly, which makes it a good starting point during interviews before optimizing.
Approach 1: Group by Sorted Characters
This approach involves separating each word into two groups of characters: those at even indices and those at odd indices. By sorting the characters in each group and forming a tuple of the sorted results, we establish a unique signature for special-equivalent strings. We then use a set to keep track of unique signatures, as duplicate tuples will represent special-equivalent strings.
The solution involves splitting each word into even-indexed and odd-indexed characters, sorting them separately, and joining the sorted results to create a signature. Unique signatures are stored in a set, representing different groups.
Complexity
Time Complexity: O(N * M log M) where N is the number of words and M is the length of each word since each word is processed and sorted.
Space Complexity: O(N * M) to store unique signatures.
Approach 2: Character Frequency Counting
This approach uses character frequency counts rather than sorting. By counting occurrences of each character in even and odd positions separately, we can determine if two strings are special-equivalent. Each string creates two frequency maps (or equivalent structures), and these are combined into a single representation for comparison across all words.
In the C solution, we count frequencies of each character at even and odd indices separately. These counts are stored in a byte array (signature) which acts as a hash map for comparing each word.
Complexity
Time Complexity: O(N * M) since we do not sort but loop through each word twice.
Space Complexity: O(N * 52) since we store only a fixed size (26 even and 26 odd).
Approach 3: Default Approach
Try this approach in the editor →Complexity Comparison
| Approach | Complexity |
|---|---|
| Group by Sorted Characters | Time Complexity: O(N * M log M) where N is the number of words and M is the length of each word since each word is processed and sorted. |
| Character Frequency Counting | Time Complexity: O(N * M) since we do not sort but loop through each word twice. |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Group by Sorted Even/Odd Characters | O(n * k log k) | O(n * k) | Simpler implementation when sorting cost is acceptable |
| Character Frequency Counting | O(n * k) | O(n) | Optimal approach when string length is large and sorting overhead should be avoided |
Video Solution
LeetCode 99 Problem 2 - Groups of Special-Equivalent Strings (893) • code_report • 2,489 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Groups of Special-Equivalent Strings easy or hard?
Groups of Special-Equivalent Strings Python/Java solution
How to solve Groups of Special-Equivalent Strings in O(n)?
What is the best approach for Groups of Special-Equivalent Strings?
Is Groups of Special-Equivalent Strings asked at Google/Amazon/Meta?
What data structure is used in Groups of Special-Equivalent Strings?
What is the time complexity of Groups of Special-Equivalent Strings?
Ready to solve this problem?
Practice Groups of Special-Equivalent Strings with our built-in code editor and test cases.
Practice on FleetCode