
swapEven => Choose any two characters that have even indexes and swap them.
swapOdd => Choose any two characters that have odd indexed and swap them.
Kevin may or may not be able to make all the strings equivalent to each other.
He can perform operations any number of times (including 0).
Strings are only composed of lowercase English alphabets.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single integer ‘N’ where ‘N’ is the total number of strings in the given set.
Next ‘N’ lines contain ‘N’ strings each separated by a new line.
For each test case, print the total number of strings that are not equivalent to any other string in the given set.
Output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of.
1 <= T <= 50
1 <= N <= 100
0 <= |S| <= 100
Time limit: 1 sec
The basic idea is to use nested loops to find which strings are distinct in the set.
For checking the equivalency of two strings, the idea is to find whether two strings have the similar number of characters (with their respective frequencies) at even indexes or not (same for odd indexes) because if any two strings have same characters at even or odd places then the characters can be swapped to form the exact similar strings.
Let’s assume two strings, s1 = “abcdefghaab” and s2 = “cfgbahedaab”.
For s1,
Characters at even indexes are = ‘a’(1), ‘b’(1), ‘d’(1), ‘f’(1), and ‘h’(1).
Characters at odd indexes are = ‘a’(2), ‘b’(1), ‘c’(1), ‘e’(1), and ‘g’(1).
For s2,
Characters at even indexes are = ‘a’(1), ‘b’(1), ‘d’(1), ‘f’(1), and ‘h’(1).
Characters at odd indexes are = ‘a’(2), ‘b’(1), ‘c’(1), ‘e’(1), and ‘g’(1).
Here, both the strings s1 and s2 have similar characters (with their frequency count) at their even and odd indexes, therefore s1 is equivalent to s2.
We can use two vectors of size 26 (one for storing frequency corresponding character at even indexes and the other for odd indexes). Steps are as follows:
The basic idea of this approach is to encode every string according to a given format and then check the number of distinct possible strings.
In the previous approach, we already know that if two strings having similar characters (with their corresponding frequencies) at even and odd places then these are equivalent to each other or we can say, we can convert one string to another by performing the given operations.