929. Groups of Special-Equivalent Strings
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the number of groups of "special-equivalent" strings. The definition of special-equivalence is key: two strings are special-equivalent if one can be transformed into the other by swapping any two even-indexed characters or any two odd-indexed characters.
Solution
Groups of Special-Equivalent Strings in Python
class Solution:
def numSpecialEquivGroups(self, words: List[str]) -> int:
# Use a set to store the canonical representations of each group.
unique_groups = set()
for word in words:
# Python's slicing with a step of 2 elegantly separates even and odd indexed characters.
# Sorting them creates a canonical representation of the multisets.
even_chars_sorted = "".join(sorted(word[0::2]))
odd_chars_sorted = "".join(sorted(word[1::2]))
# A tuple of these two sorted strings serves as a unique, hashable key
# for the special-equivalent group.
signature = (even_chars_sorted, odd_chars_sorted)
unique_groups.add(signature)
# The length of the set is the number of unique groups.
return len(unique_groups)
Groups of Special-Equivalent Strings in Java
class Solution {
public int numSpecialEquivGroups(String[] words) {
// Use a HashSet to store the canonical representations of each group.
Set<String> uniqueGroups = new HashSet<>();
for (String word : words) {
// Create frequency count arrays for even and odd indexed characters.
int[] evenCounts = new int[26];
int[] oddCounts = new int[26];
// Populate the frequency arrays.
for (int i = 0; i < word.length(); i++) {
if (i % 2 == 0) {
evenCounts[word.charAt(i) - 'a']++;
} else {
oddCounts[word.charAt(i) - 'a']++;
}
}
// Create a canonical string representation from the count arrays.
// Arrays.toString() provides a consistent, unique string like "[0, 1, ..., 0]".
// Concatenating both gives a unique signature for the special-equivalent group.
String signature = Arrays.toString(evenCounts) + Arrays.toString(oddCounts);
uniqueGroups.add(signature);
}
// The size of the set is the number of unique groups.
return uniqueGroups.size();
}
}
Groups of Special-Equivalent Strings in C++
class Solution {
public:
int numSpecialEquivGroups(vector<string>& words) {
// Use a hash set to store the canonical representations of each group.
unordered_set<string> unique_groups;
for (const string& word : words) {
// Create two frequency count arrays for even and odd indexed characters.
vector<int> even_counts(26, 0);
vector<int> odd_counts(26, 0);
// Populate the frequency arrays.
for (int i = 0; i < word.length(); ++i) {
if (i % 2 == 0) {
even_counts[word[i] - 'a']++;
} else {
odd_counts[word[i] - 'a']++;
}
}
// Create a canonical string representation from the counts.
// This signature uniquely identifies a special-equivalent group.
string signature = "";
for (int count : even_counts) {
// Using a separator to avoid ambiguity, e.g., counts 1, 12 vs 11, 2.
signature += to_string(count) + ",";
}
signature += "|"; // Separator between even and odd counts
for (int count : odd_counts) {
signature += to_string(count) + ",";
}
// Add the canonical representation to the set.
unique_groups.insert(signature);
}
// The size of the set is the number of unique groups.
return unique_groups.size();
}
};
Groups of Special-Equivalent Strings in TypeScript
function numSpecialEquivGroups(words: string[]): number {
// Use a Set to store the canonical representations of each group.
const uniqueGroups = new Set<string>();
for (const word of words) {
// Create frequency count arrays for even and odd indexed characters.
const evenCounts = new Array(26).fill(0);
const oddCounts = new Array(26).fill(0);
// Populate the frequency arrays.
for (let i = 0; i < word.length; i++) {
const charCode = word.charCodeAt(i) - 'a'.charCodeAt(0);
if (i % 2 === 0) {
evenCounts[charCode]++;
} else {
oddCounts[charCode]++;
}
}
// Create a canonical string representation from the count arrays.
// Joining the arrays with a separator creates a unique key for the set.
const signature = evenCounts.join(',') + '|' + oddCounts.join(',');
uniqueGroups.add(signature);
}
// The size of the set is the number of unique groups.
return uniqueGroups.size;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.