#893

Groups of Special-Equivalent Strings

pupil · 585 · lc medium +29 · verified · 73.5% accepted · 571 likes · top 84%

Description

Given an array of equal-length strings words, two strings are special-equivalent if one can be turned into the other by any sequence of swaps between same-parity positions (both even-indexed or both odd-indexed).

Return the number of distinct maximal groups where every pair of strings inside the group is special-equivalent.

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

Code

1
2
3