package leetcode;

/**
 * https://leetcode.com/problems/swap-for-longest-repeated-character-substring/
 */
public class Problem1156 {
    public int maxRepOpt1(String text) {
        // TODO
        return 0;
    }

    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("aaaaa")); // 5
        System.out.println(prob.maxRepOpt1("abcdef")); // 1
    }
}

@@ -12,16 +12,90 @@
 tcode;%0A%0A
+import java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -254,32 +254,1856 @@
     
-// TODO%0A        return 0
+int%5B%5D%5B%5D counts = new int%5Btext.length()%5D%5B26%5D;%0A        for (int i = text.length() - 1; i %3E= 0; i--) %7B%0A            int%5B%5D newArray = (i %3C text.length() - 1) ? Arrays.copyOf(counts%5Bi + 1%5D, 26) : new int%5B26%5D;%0A            newArray%5Btext.charAt(i) - 'a'%5D++;%0A            counts%5Bi%5D = newArray;%0A        %7D%0A//        for (int%5B%5D c : counts) %7B%0A//            System.out.println(Arrays.toString(c));%0A//        %7D%0A        Set%3CInteger%3E cuts = new HashSet%3C%3E();%0A        for (int i = 0; i %3C text.length() - 1; i++) %7B%0A            if (text.charAt(i) != text.charAt(i + 1)) %7B%0A                cuts.add(i);%0A                cuts.add(i + 1);%0A            %7D%0A        %7D%0A        if (cuts.isEmpty()) %7B%0A            return text.length();%0A        %7D%0A//        System.out.println(cuts);%0A        int answer = 0;%0A        for (int i = 0; i %3C cuts.size(); i++) %7B%0A            boolean swapped = false;%0A            int length = 1;%0A            int j = i;%0A            char current = text.charAt(j);%0A            while (j %3C text.length() - 1) %7B%0A                if (current == ' ') %7B%0A                    current = text.charAt(j);%0A                %7D%0A                char next = text.charAt(j + 1);%0A                if (current != next) %7B%0A                    // Check if it's swappable and there is a character to swap.%0A                    if (swapped %7C%7C counts%5Bj + 1%5D%5Bcurrent - 'a'%5D == 0) %7B%0A                        break;%0A                    %7D%0A                    swapped = true;%0A                    // If we swap, the next char will be the same as current char.%0A                %7D else %7B%0A                    if (swapped && counts%5Bj + 1%5D%5Bcurrent - 'a'%5D == 1) %7B%0A                        break;%0A                    %7D%0A                    current = ' ';%0A                %7D%0A                j++;%0A                length++;%0A            %7D%0A            answer = Math.max(answer, length);%0A        %7D%0A        return answer
 ;%0A  
@@ -2194,24 +2194,26 @@
 blem1156();%0A
+//
         Syst
@@ -2259,16 +2259,18 @@
 ); // 3%0A
+//
         
@@ -2322,16 +2322,18 @@
 ); // 6%0A
+//
         
@@ -2386,16 +2386,83 @@
 ); // 4%0A
+//        System.out.println(prob.maxRepOpt1(%22aaabaaaca%22)); // 7%0A//
         
@@ -2512,16 +2512,18 @@
 ); // 5%0A
+//
         
@@ -2574,16 +2574,264 @@
 ); // 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

Update problem 1156