Groups Of Special-Equivalent Strings Problem
Description
LeetCode Problem 893.
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" and words[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 that words[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 <= 1000
- 1 <= words[i].length <= 20
- words[i] consist of lowercase English letters.
- All the strings are of the same length.
Solution
Approach: Canonical Signature in a Hash Set
A move swaps two letters that are both at even indices, or both at odd indices. Repeated swaps can put the even-indexed letters in any order, and the odd-indexed letters in any order, but a letter can never move from an even index to an odd one. So two words are special-equivalent exactly when they have the same multiset of even-indexed letters and the same multiset of odd-indexed letters.
That gives each word a signature that is the same for every word in its group and different for every other group:
- Collect the letters at even indices and sort them.
- Collect the letters at odd indices and sort them.
- Join the two sorted strings and add the result to a hash set.
The answer is the number of distinct signatures. Because the equivalence is decided by the signature alone, the groups are exactly the sets of words that share one, and no pairwise comparison is needed.
For "zzxy", the even letters are z, x and the odd letters are z, y, giving "xz" + "yz". "zzyx" gives "yz" + "xz", which is different, so the two words are in different groups.
All words have the same length, so the even part always has the same length too, and the two parts can be joined without a separator.
Complexity: O(n · m log m) time and O(n · m) space, where n is the number of words and m is their length.
class Solution:
def numSpecialEquivGroups(self, words: List[str]) -> int:
signatures = set()
for word in words:
# Letters can move freely among even positions and among odd positions,
# so a word is described by its sorted even letters and sorted odd letters.
evens = "".join(sorted(word[0::2]))
odds = "".join(sorted(word[1::2]))
signatures.add(evens + odds) # all words have the same length, so no separator is needed
return len(signatures)
Now write it from memory
Close the write-up and solve it yourself against the tests.