Groups of Special-Equivalent Strings - Easy - LeetCode
💻 coding

Groups of Special-Equivalent Strings - Easy - LeetCode

2 min read 333 words
2 min read
ShareWhatsAppPost on X
  • 1Special-equivalent strings allow swapping of even or odd indexed characters to achieve equivalence.
  • 2The problem requires identifying the largest groups of strings that are special-equivalent.
  • 3The solution involves using a HashSet to count unique combinations of sorted even and odd indexed characters.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"Special-equivalent strings allow swapping of even or odd indexed characters to achieve equivalence."

Groups of Special-Equivalent Strings - Easy - LeetCode

You are given an array A of strings.

A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S.

Two strings S and T are special-equivalent if after any number of moves onto S, S == T.

For example, S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz" that swap S[0] and S[2], then S[1] and S[3].

Now, a group of special-equivalent strings from A is a non-empty subset of A such that:

Every pair of strings in the group are special equivalent, and;

The group is the largest size possible (ie., there isn't a string S not in the group such that S is special equivalent to every string in the group)

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

Example 1:

Input: ["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 are 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: ["abc","acb","bac","bca","cab","cba"]

Output: 3

Note:

1 <= A.length <= 1000

1 <= A[i].length <= 20

All A[i] have the same length.

All A[i] consist of only lowercase letters.

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCode.AskGif.Easy.String
{
 class NumSpecialEquivGroupsSoln
 {
 public void execute()
 {
 var input1 = new string[]{"abcd", "cdab", "cbad", "xyzz", "zzxy", "zzyx"};
 var res = NumSpecialEquivGroups(input1);
 }

 public int NumSpecialEquivGroups(string[] A)
 {
 var set = new HashSet<string>();
 for (int i = 0; i < A.Length; i++)
 {
 var even = new StringBuilder();
 var odd = new StringBuilder();
 for (int j = 0; j < A[i].Length; j++)
 { 
 if (j % 2 == 0)
 even.Append(A[i][j]);
 else
 odd.Append(A[i][j]);
 }

 var evenStr = string.Concat(even.ToString().OrderBy(c => c));
 var oddStr = string.Concat(odd.ToString().OrderBy(c => c));

 set.Add(evenStr + oddStr);
 }

 return set.Count;
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 6 June 2020 · 2 min read · 333 words

Part of AskGif Blog · coding

You might also like