https://leetcode.com/problems/groups-of-special-equivalent-strings/
https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163413/Java-Concise-Set-Solution
https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163547/Python-1-liner
You are given an array
A
of strings.
Two strings
S
and T
are special-equivalent if after any number of moves, S == T.
A move consists of choosing two indices
i
and j
with i % 2 == j % 2
, and swapping S[i]
with S[j]
.
Now, a group of special-equivalent strings from
A
is a non-empty subset S of A
such that any string not in S is not special-equivalent with any string in S.
Return the number of groups of special-equivalent strings from
A
.
For each String, we generate it's corresponding signature, and add it to the set.
In the end, we return the size of the set.
In the end, we return the size of the set.
public int numSpecialEquivGroups(String[] A) {
Set<String> set= new HashSet<>();
for (String s: A){
int[] odd= new int[26];
int[] even= new int[26];
for (int i=0; i<s.length(); i++){
if (i%2==1) odd[s.charAt(i)-'a']++;
else even[s.charAt(i)-'a']++;
}
String sig= Arrays.toString(odd)+Arrays.toString(even);
set.add(sig);
}
return set.size();
}
https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163767/Straightforward-Java-solution
map1 - the distribution of counts of the characters at even positions
map2 - the distribution of counts of the characters at odd positions
set - how many unique distributions
map2 - the distribution of counts of the characters at odd positions
set - how many unique distributions
https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163547/Python-1-liner