Groups of Special-Equivalent Strings

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].

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.
  • The group is the largest size possible, meaning there is not a string words[i] outside 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
Inputwords = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
Output3
The groups are ["abcd", "cdab", "cbad"], ["xyzz", "zzxy"], and ["zzyx"], so there are 3 groups.
Example 2
Inputwords = ["abc","acb","bac","bca","cab","cba"]
Output3
The words form 3 distinct groups of special-equivalent strings.

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.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate