package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/swap-for-longest-repeated-character-substring/
 */
public class Problem1156 {
    public int maxRepOpt1(String text) {
        List<Integer>[] charIndexes = new ArrayList[26];
        for (int i = 0; i < 26; i++) {
            charIndexes[i] = new ArrayList<>();
        }
        int n = text.length();
        for (int i = 0; i < n; i++) {
            charIndexes[text.charAt(i) - 'a'].add(i);
        }
        int answer = 1;
        for (int i = 0; i < 26; i++) {
            int current = 1;
            int previous = 0;
            int max = 0;
            for (int j = 1; j < charIndexes[i].size(); j++) {
                if (charIndexes[i].get(j) == charIndexes[i].get(j - 1) + 1) {
                    current++;
                } else {
                    previous = charIndexes[i].get(j) == charIndexes[i].get(j - 1) + 2 ? current : 0;
                    current = 1;
                }
                max = Math.max(max, current + previous);
            }
            answer = Math.max(answer, max + (charIndexes[i].size() > max ? 1 : 0));
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1156 prob = new Problem1156();
//        System.out.println(prob.maxRepOpt1("ababa")); // 3
//        System.out.println(prob.maxRepOpt1("aaabaaa")); // 6
        System.out.println(prob.maxRepOpt1("aaabbaaa")); // 4
//        System.out.println(prob.maxRepOpt1("aaabaaaca")); // 7
//        System.out.println(prob.maxRepOpt1("aaaaa")); // 5
//        System.out.println(prob.maxRepOpt1("abcdef")); // 1
//        System.out.println(prob.maxRepOpt1("abcdefa")); // 2
//        System.out.println(prob.maxRepOpt1("abcaaa")); // 3
//        System.out.println(prob.maxRepOpt1("aaa")); // 3
//        System.out.println(prob.maxRepOpt1("bbababaaaa")); // 6
    }
}

@@ -1199,729 +1199,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1156 prob = new Problem1156();%0A//        System.out.println(prob.maxRepOpt1(%22ababa%22)); // 3%0A//        System.out.println(prob.maxRepOpt1(%22aaabaaa%22)); // 6%0A        System.out.println(prob.maxRepOpt1(%22aaabbaaa%22)); // 4%0A//        System.out.println(prob.maxRepOpt1(%22aaabaaaca%22)); // 7%0A//        System.out.println(prob.maxRepOpt1(%22aaaaa%22)); // 5%0A//        System.out.println(prob.maxRepOpt1(%22abcdef%22)); // 1%0A//        System.out.println(prob.maxRepOpt1(%22abcdefa%22)); // 2%0A//        System.out.println(prob.maxRepOpt1(%22abcaaa%22)); // 3%0A//        System.out.println(prob.maxRepOpt1(%22aaa%22)); // 3%0A//        System.out.println(prob.maxRepOpt1(%22bbababaaaa%22)); // 6%0A    %7D
 %0A%7D%0A

Solve problem 1156