893. Groups of Special-Equivalent Strings

Difficulty:
Related Topics:
Similar Questions:

    Problem

    You are given an array of strings of the same length words.

    In one move, you can swap any two even indexed characters or any two odd indexed characters of a string words[i].

    Two strings words[i] and words[j] are special-equivalent if after any number of moves, words[i] == words[j].

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

    Return **the number of *groups of special-equivalent strings* from **words.

      Example 1:

    Input: words = ["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 is 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: words = ["abc","acb","bac","bca","cab","cba"]
    Output: 3
    

      Constraints:

    Solution (Java)

    class Solution {
        public int numSpecialEquivGroups(String[] words) {
            HashSet<String> set = new HashSet<>();
            int result = 0;
            for (String str : words) {
                if (set.add(getHashBySwap(str.toCharArray()))) {
                    result++;
                }
            }
            return result;
        }
    
        private String getHashBySwap(char[] chars) {
            for (int i = 0; i < chars.length; i++) {
                int j = i + 2;
                while (j < chars.length) {
                    if (chars[i] > chars[j]) {
                        char temp = chars[j];
                        chars[j] = chars[i];
                        chars[i] = temp;
                    }
                    j += 2;
                }
            }
            return String.valueOf(chars);
        }
    }
    

    Explain:

    nope.

    Complexity: