

“code” and “eodc” are similar because we can swap letters at positions 0 and 3 in ‘code’ to get “eodc”.
Group of strings [“code”, “eodc”, “edoc”] is a Similar String Group, because “code” is similar to “eodc”, “eodc” is similar to both “code” and “edoc” and “edoc” is similar to “eodc” i.e each string is similar to at least one other string of group.
1. Two strings are an anagram of each other if they have the same characters but these characters can be arranged in a different order. For example, “LISTEN” and “SILENT” are anagrams.
Consider ‘STRS’ = [“code”, “doce”, ”code”, “ceod”, “eodc”, “edoc”, “odce”].
There are 2 Similar String Groups in ‘STRS’ -:
1. “code”, “code”, “doce“, “odce”, eodc”, “edoc”
2. “ceod”
Thus, we should return 2 in this case.
The first line of input contains an integer ‘T’ denoting the number of test cases. then ‘T’ test cases follow.
The first line of each test case consists of a single integer ‘N’ representing the size of the list/array ‘STRS’.
The second line of each test case consists of ‘N’ single space-separated strings representing strings in list/array ‘STRS’.
For each test case, print a single integer representing the number of Similar String Groups in ‘STRS’.
Print output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 100
1 <= |STRS[i]| <= 5
‘STRS[i]’ has only lowercase english letters.
Time limit: 1 sec
Consider that each string in ‘STRS’ represents a node in the graph, and there is an undirected edge between two nodes if strings represented by them are similar. After that, the problem reduces to finding the number of connected components in a graph, which can be solved either by Depth First Search or Breadth-First Search.
A naive algorithm based on Depth First Search (DFS), in which for each string in ‘STRS’ we one by one check for other string whether it is similar to it or not is described below -:
We can optimize the previous approach, by using the fact that for a string of length |S| there can be at most (|S| * (|S| - 1)) / 2 similar strings other than it. We can find all these strings by swapping each pair of characters.