Groups of Special-Equivalent Strings - Problem
Groups of Special-Equivalent Strings

You are given an array of strings words where all strings have the same length. Here's the twist: you can perform special swaps within each string!

Special Swap Rules:
• You can swap any two characters at even indices (0, 2, 4, ...)
• You can swap any two characters at odd indices (1, 3, 5, ...)
• You can perform these swaps as many times as you want

Two strings are special-equivalent if you can transform one into the other using these special swaps. For example, "zzxy" and "xyzz" are special-equivalent because:
"zzxy" → "xzzy" (swap even indices 0,2)
"xzzy" → "xyzz" (swap odd indices 1,3)

Your Goal: Find the number of groups where each group contains all strings that are special-equivalent to each other, and no string from different groups can be special-equivalent.

Input & Output

example_1.py — Basic Example
$ Input: ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
Output: 3
💡 Note: Group 1: ["abcd","cdab","cbad"] - all have even chars [a,c] and odd chars [b,d]. Group 2: ["xyzz","zzxy"] - both have even chars [x,z] and odd chars [y,z]. Group 3: ["zzyx"] - has even chars [z,y] and odd chars [z,x], which is unique.
example_2.py — All Same Group
$ Input: ["abc","acb","bac","bca","cab","cba"]
Output: 3
💡 Note: For length 3 strings: Group 1: ["abc","bac"] (even:[a,c], odd:[b]). Group 2: ["acb","cab"] (even:[a,b], odd:[c]). Group 3: ["bca","cba"] (even:[b,a], odd:[c]).
example_3.py — Single Characters
$ Input: ["a","b","c"]
Output: 3
💡 Note: Each single character string forms its own group since they have different even characters and no odd characters.

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
🃏 Card Deck Analogy: Special-Equivalent StringsOriginal Card Decks (Strings):Deck 1: "abcd"abcdPos:0123Deck 2: "cdab"cdabPos:0123Step 1: Separate Even/Odd Position CardsDeck 1 Even:acDeck 1 Odd:bdDeck 2 Even:caDeck 2 Odd:dbStep 2: Sort Each Group IndependentlySorted Even:acSorted Odd:bdSorted Even:acSorted Odd:bdStep 3: Create SignaturesDeck 1 Signature:"ac|bd"Deck 2 Signature:"ac|bd"Same Group!Both decks haveidentical signatures
Understanding the Visualization
1
Separate Card Positions
For each deck, separate cards at even and odd positions into two groups
2
Sort Each Group
Sort the even-position cards and odd-position cards independently
3
Create Deck Signature
Combine the sorted groups to create a unique fingerprint for each deck
4
Count Unique Signatures
Decks with the same signature belong to the same group
Key Takeaway
🎯 Key Insight: Two strings are special-equivalent if and only if they have the same sorted characters at even positions AND the same sorted characters at odd positions. This allows us to create unique signatures and count groups efficiently!
Asked in
Google 15 Amazon 8 Microsoft 5 Facebook 3
28.0K Views
Medium Frequency
~12 min Avg. Time
850 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