leetcode (Groups of Special-Equivalent Strings)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85226581

Title:Groups of Special-Equivalent Strings    893

Difficulty:Easy

原题leetcode地址: https://leetcode.com/problems/groups-of-special-equivalent-strings/

1.  将对应的字符出现的次数存放到数组中(奇偶位置分开存放),将每一次存放的数组存放到set集合中

时间复杂度:O(n^2),嵌套for循环。

空间复杂度:O(n),申请了Set和数组。

    /**
     * 将对应的字符出现的次数存放到数组中(奇偶位置分开存放),将每一次存放的数组存放到set集合中
     * @param A
     * @return
     */
    public static int numSpecialEquivGroups(String[] A) {

        Set<String> set = new HashSet<>();

        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();

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85226581
今日推荐