Loading

LEETCODE-1156-单字符重复子串的最大长度

题目参考:https://leetcode.cn/problems/swap-for-longest-repeated-character-substring/
题解参考:https://leetcode.cn/problems/swap-for-longest-repeated-character-substring/solution/zhao-dao-mo-shi-jiu-hen-jian-dan-by-brya-x6k4/

我的题解

class Solution {
    public int maxRepOpt1(String text) {

        int gMax = 0;
        int max = 0;

        // 以下只是没换的一种情况,对换了字符串可能会有很多情况
        for (int i = 0; i < text.split("").length - 1; i++) {
            for (int j = i + 1; j < text.split("").length; j++) {
                int left = 0;
                int right = 0;
                int count = 0;
                String[] ss = text.split("");

                String tmp;
                // 相同的话就不交换
                if (Objects.equals(ss[i], ss[j])) {
                    continue;
                }
                // 得到一个交换后的全新的数组
                tmp = ss[i];
                ss[i] = ss[j];
                ss[j] = tmp;

                Map<String, Integer> map = new HashMap<>(ss.length);
                for (String s : ss) {
                    map.put(s, 0);
                }

                // 滑窗右边界右移 ababa
                while (right < ss.length) {
                    String s = ss[right];
                    if (map.get(s) == 0) {
                        count++;
                    }
                    map.put(ss[right], map.get(ss[right]) + 1);

                    // 条件(单字符重复)是否满足
                    while (count > 1) {
                        if (map.get(ss[left]) == 1) {
                            count--;
                        }
                        map.put(ss[left], map.get(ss[left]) - 1);
                        left++;
                    }
                    gMax = Math.max(gMax, right - left + 1);
                    right++;
                }

                max = Math.max(max, gMax);
            }
        }

        return max == 0 ? text.length() : max;
    }
}

其他题解

class Solution {
    public int maxRepOpt1(String text) {

        if (text == null || text.length() == 0) {
            return 0;
        }

        char[] chars = text.toCharArray();
        int[] count = new int[26];
        for (char c : chars) {
            count[c - 'a']++;
        }

        // 字符串长度
        int length = chars.length;
        // 当前字符
        char c = chars[0];
        // 当前字符出现的次数
        int num = 1;
        // 最大的连续长度(至少都有一个)
        int max = 1;

        for (int i = 1; i < length; i++) {
            if (chars[i] == c) {
                // 从第一个字符开始统计
                num++;
            } else {
                // 允许中间有一个不一样的字符出现
                int j = i + 1;
                while (j < length && chars[j] == c) {
                    j++;
                    num++;
                }

                // 在连续序列之外(不管是前是后)出现了同样的字符,那么再+1
                if (count[c - 'a'] > num) {
                    num++;
                }

                max = Math.max(max, num);

                // 继续统计下一个字符
                c = chars[i];
                num = 1;
            }
        }

        // 等到最后一个字符统计完毕后,再进行一次判断统计,原理同之前一样
        if (count[c - 'a'] > num) {
            num++;
        }

        max = Math.max(max, num);
        return max;

    }
}

posted @ 2022-08-02 09:53  溫柔の風  阅读(25)  评论(0编辑  收藏  举报