LeetCode.893- special string equality groups (Groups of Special-Equivalent Strings)

This is the first book of pleasure and delight 344 update, the first 368 Pian original

01 questions and look ready

Introduced today is the LeetCodearithmetic problem in Easythe first-level 209title (overall title number is 893).
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.

Given a string array A.

If after any number of mobile, two strings S and T are equal special S == T.

It refers to selecting two moving indices i and j, where i%2 == j%2, and by S[j]exchange S[i].

Now, the string S is equal to a special group, A is a non-empty set, so that in the absence of S (but present in A) is an arbitrary string and any string S is not a special equal.

Returns A special group of an equal number string. E.g:

Input: [ "a", "b ", "c", "a", "c", "c"]
Output: 3
Description: 3 Group [ "a", "a" ], [ "b"], [ "c", "c" , "c"]

Input: [ "aa", "bb ", "ab", "ba"]
Output: 4
Description: 4 Group [ "aa"], [ " bb"], [ "ab"], [ "ba"]

Input: [ "abc", "acb ", "bac", "bca", "cab", "cba"]
Output: 3
Description: 3 Group [ "abc", "cba" ], [ "acb", " bca "], [" bac " ," cab "]

Input: [ "abcd", "cdab" , "adcb", "cbad"]
Output: 1
: 1 Group [ "abcd", "cdab" , "adcb", "cbad"]

Note :

  • 1 <= A.length <= 1000

  • 1 <= A [i] .length <= 20

  • All A [i] have the same length.

  • All A [i] contains only lowercase letters.

02 understanding the problem

At first look at the title of this question is to look to see the description ignorant force, do not know to express what it means, let alone how to solve problems up.

Can only bite the bullet and read, read not remember how many times, with its four examples given, finally understand the meaning of it wants to say, the Chinese described above is what I do on the basis of the translation on Google polish, easy to understand, nothing and people can directly see the English description, you can see the wonder of life!

Topic that two strings S and J can be any exchange of characters, but there is a premise i%2 == j%2, that is to say only the odd bit S and the odd bit change, only even bit S and even bit exchange, the exchange J Similarly, If J is equal to S, and after completion of change is equal special.

Now we have the concept of a special group of strings are equal, is the equal of those special string of small gangs, each gang inside a small string, are in line with the above conversion rules. Q A, how many such small gangs ?

Four cases in terms of binding, and soon you will understand what the topic is God horse mean.

The fourth example, A = [“abcd”,“cdab”,“adcb”,“cbad”]and finally the output is equal to the number of special strings set for 1, let's take a look at how this one is counted out.

First to "abcd"convert one, 'a', 'c'interchangeable 'b', 'd'interchangeable, since the subject is said any swap, meaning that regardless of the order, then we are unified in ascending order, after the completion of the conversion becomes "ac" + "bd"="acbd".

Following this logic, a "cdab", "adcb", "cbad"after conversion, have become "acbd", so that finally there is only one gang, that is "acbd".

03 The first solution

When the processing of each individual string, using a 26-bit length of the intarray, recording the number of each character occurring on odd bit, even-dimensional separately, and then transferred into a new array of two strings spliced together, deposit HashSet, the number of the last special group is equal to the string HashSetof sizethe.

public int numSpecialEquivGroups(String[] A) {
    Set<String> set = new HashSet<String>();
    for (String str : A) {
        int[] odd = new int[26];
        int[] even = new int[26];
        for (int i=0; i<str.length(); i++) {
            if (i%2 == 0) {
                even[str.charAt(i)-'a']++;
            } else {
                odd[str.charAt(i)-'a']++;
            }
        }
        String newStr = Arrays.toString(odd)+Arrays.toString(even);
        set.add(newStr);
    }
    return set.size();
}


04 The second solution

We can further optimize the solution to the above, only one int array, but the length becomes 52, the first half of memory even bit, odd bit the second half of the village, then into the last string, stored HashSetin the last special characters equal is the number of strings HashSetof sizethe.

public int numSpecialEquivGroups2(String[] A) {
    Set<String> set = new HashSet<String>();
    for (String str : A) {
        int[] count = new int[52];
        for (int i=0; i<str.length(); i++) {
            count[str.charAt(i)-'a'+26*(i%2)]++;
        }
        set.add(Arrays.toString(count));
    }
    return set.size();
}


05 Summary

Thematic algorithm has been continuous days more than six months , the algorithm of feature articles 212 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures either] in a keyword, to obtain a series of articles Collection .

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!

Guess you like

Origin www.cnblogs.com/xiaochuan94/p/10977512.html