Minimum Number of String Groups Through Transformations Problem
Description
LeetCode Problem 3999.
You are given an array of strings words.
Define a transformation on a string s as follows:
- Let
Ebe the subsequence of characters at even indices ofs. - Let
Obe the subsequence of characters at odd indices ofs. - Independently cyclically shift
EandOby any number of positions to the right, possibly zero. - Reconstruct the string by placing the shifted
Echaracters back into even indices and the shiftedOcharacters back into odd indices.
Two strings are equivalent if one can be transformed into the other by a single transformation.
Partition words into the minimum number of groups such that:
- Every string belongs to exactly one group.
- Every pair of strings in the same group are equivalent.
Return an integer denoting the minimum number of groups.
Example 1:
Input: words = ["ntgwz","zwntg"]
Output: 1
Explanation:
- For "ntgwz", the even-index subsequence is "ngz" and the odd-index subsequence is "tw".
- Shift "ngz" right by 1 position to obtain "zng", and shift "tw" right by 1 position to obtain "wt".
- After reconstructing the string, we obtain "zwntg".
- Therefore, both strings are equivalent and belong to the same group.
Example 2:
Input: words = ["abc","cab","bac","acb","bca","cba"]
Output: 3
Explanation:
The strings can be partitioned into the following groups:
- ["abc","cba"]
- ["cab","bac"]
- ["acb","bca"]
Example 3:
Input: words = ["leet","abb","bab","deed","edde","code","bba"]
Output: 5
Explanation:
The strings can be partitioned into the following groups:
- ["abb","bba"]
- ["deed","edde"]
- ["leet"]
- ["bab"]
- ["code"]
All pairs of strings in each group are equivalent.
Constraints:
1 <= words.length <= 10^51 <= words[i].length <= 5 * 10^5- The sum of
words[i].lengthdoes not exceed5 * 10^5. words[i]consist of lowercase English letters.
Solution
Approach: Canonical Rotation
A transformation rotates the even-indexed characters and the odd-indexed characters independently, so two words are equivalent exactly when their even parts are rotations of each other and their odd parts are rotations of each other. This relation is symmetric and transitive, so the minimum number of groups is simply the number of distinct equivalence classes.
To name a class, take the lexicographically smallest rotation of the even part and of the odd part (the minimal rotation) and combine the two into a key. Rotations of one string share the same minimal rotation. The minimal rotation can be found in linear time with the standard two-pointer method: keep two candidate start positions i and j and a match length k; on a mismatch the larger candidate (and the k positions after it) is discarded.
Put all keys in a hash set and return its size.
Complexity: time O(L), space O(L), where L is the total length of all words.
class Solution:
def minimumGroups(self, words: List[str]) -> int:
def min_rotation(t: str) -> str:
n = len(t)
i, j, k = 0, 1, 0
while i < n and j < n and k < n:
a = t[(i + k) % n]
b = t[(j + k) % n]
if a == b:
k += 1
continue
if a > b:
i += k + 1
else:
j += k + 1
if i == j:
j += 1
k = 0
p = min(i, j)
return t[p:] + t[:p]
seen = set()
for w in words:
seen.add((min_rotation(w[0::2]), min_rotation(w[1::2])))
return len(seen)