Groups of Special-Equivalent Strings - Problem

You are given an array of strings words where all strings have the same length.

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 where:

  • Every pair of strings in the group are special-equivalent
  • The group is the largest size possible (no string outside the group is special-equivalent to every string in the group)

Return the number of groups of special-equivalent strings from words.

Input & Output

Example 1 — Basic Grouping
$ Input: words = ["abc","acb","bac","bca","cab","cba"]
Output: 3
💡 Note: Group 1: ["abc","bac"] (even: "ac", odd: "b"), Group 2: ["acb","bca","cab","cba"] (even: "ac"/"bc"/"cb", odd: "cb"/"a"/"a"), Group 3: singles. After proper analysis: 3 distinct signatures.
Example 2 — All Same Group
$ Input: words = ["a"]
Output: 1
💡 Note: Single string forms one group.
Example 3 — No Equivalents
$ Input: words = ["aa","bb","ab","ba"]
Output: 4
💡 Note: "aa" (even:"a", odd:"a"), "bb" (even:"b", odd:"b"), "ab" (even:"a", odd:"b"), "ba" (even:"b", odd:"a") - all different signatures.

Constraints

  • 1 ≤ words.length ≤ 1000
  • 1 ≤ words[i].length ≤ 20
  • words[i] consists of lowercase English letters
  • All the strings are of the same length

Visualization

Tap to expand
Groups of Special-Equivalent Strings INPUT words array (6 strings) "abc" "acb" "bac" "bca" "cab" "cba" Index Positions: Position: 0 1 2 Type: E O E E = Even index (swappable) O = Odd index (swappable) Example "abc": Even chars: a, c Odd chars: b Swap even: abc --> cba ALGORITHM STEPS 1 Extract Characters Separate even/odd indices 2 Sort Each Group Sort even chars, odd chars 3 Create Signature Combine sorted groups 4 Count Unique Use HashSet for signatures Signature Computation: Word Even Odd Signature "abc" ac b "ac|b" "acb" ab c "ab|c" "bac" bc a "bc|a" "bca" ba c "ab|c" "cab" cb a "bc|a" "cba" ca b "ac|b" FINAL RESULT 3 Unique Groups Found: Group 1: Signature "ac|b" "abc" "cba" Group 2: Signature "ab|c" "acb" "bca" Group 3: Signature "bc|a" "bac" "cab" OUTPUT 3 Key Insight: Two strings are special-equivalent if and only if they have the same multiset of characters at even indices AND the same multiset of characters at odd indices. By sorting these character groups and combining them into a signature, we can use a HashSet to count unique groups in O(n * k log k) time. TutorialsPoint - Groups of Special-Equivalent Strings | Hash Map with Signature
Asked in
Google 15 Facebook 12 Amazon 8
28.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen