Distinct Strings With Odd and Even Swapping Allowed

You are given an array of strings. Your task is to find the number of unique strings.

A string is considered unique if it cannot be formed from any other string, present in the array, even after applying the following operations any number of times and in any order:

1. Swapping two characters present at odd indices.

2. Swapping two characters present at even indices.

Note:
Strings contain only lowercase English alphabets.
Example:
Let the array of strings be [“abcd”, “cbad”, “bdac”, “adcb”]. From the given array, strings “abcd”, “cbad”, and “adcb” can be transformed into one another by applying the given operations. But “bdac” cannot be transformed into any other string. Hence, there are only 2 unique strings in the array.
Input format:
The very first line of input contains an integer ‘T’ denoting the number of test cases. 

The first line of every test case contains an integer ‘N’ denoting the number of strings present in the array.

The second line of every test case contains ‘N’ space-separated strings, which are present in the array.
Output format:
For each test case, the number of unique strings present in the array is printed.

Output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just return the number of unique strings. 
Constraints:
1 <= T <= 10 
1 <= N <= 2000
1 <= Length of String <= 1000

Time Limit: 1 sec

Where  ‘T’ represents the number of test cases and ‘N’ represents the number of strings present in the array.
CodingNinjas
author
2y
Encoding String Using Sorting
  • The idea behind this approach is to encode every string using the even and odd index characters of the string.
  • For any given string, we split the string into two substrings...read more
CodingNinjas
author
2y
Encoding String And Hashing
  • We can avoid sorting the strings by using a different approach for encoding.
  • The idea is to generate the encoded string using the frequency of each character from ‘a’ to ‘z’ ...read more
Help your peers!
Add answer anonymously...
Tech Mahindra Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter