package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/reorganize-string/
 */
public class Problem767 {
    public String reorganizeString(String S) {
        CharCount[] array = new CharCount[26];
        for (char i = 0; i < array.length; i++) {
            array[i] = new CharCount((char) (i + 'a'), 0);
        }
        for (int i = 0; i < S.length(); i++) {
            array[S.charAt(i) - 'a'].count++;
        }
        Arrays.sort(array, (a, b) -> Integer.compare(b.count, a.count));
        StringBuilder newString = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].count; j++) {
                newString.append(array[i].ch);
            }
        }
        StringBuilder sb = new StringBuilder();
        int i = 0;
        int j = 0;
        char[] chars = newString.toString().toCharArray();
        while (j < chars.length) {
            if (chars[i] == chars[j]) {
                j++;
            } else {
                sb.append(chars[i++]).append(chars[j++]);
            }
        }
//        sb.append(chars[i]);
//        if (i + 1 < S.length() && chars[i] == chars[i + 1]) {
//            return "";
//        }
        return sb.toString();
    }

    private static class CharCount {
        private char ch;
        private int count;

        public CharCount(char ch, int count) {
            this.ch = ch;
            this.count = count;
        }

        @Override
        public String toString() {
            return ch + " --> " + count;
        }
    }

    public static void main(String[] args) {
        Problem767 prob = new Problem767();
//        System.out.println(prob.reorganizeString("aab")); // aba
//        System.out.println(prob.reorganizeString("aaab")); //
//        System.out.println(prob.reorganizeString("aaabb")); // ababa
        System.out.println(prob.reorganizeString("aabbb")); // babab
//        System.out.println(prob.reorganizeString("aaabbb")); // ababab
    }
}

@@ -1087,26 +1087,24 @@
       %7D%0A
-//
         
 sb.appen
@@ -1099,48 +1099,68 @@
     
-sb.append(chars%5Bi%5D);%0A//        if (i + 1
+char c = chars%5Bi++%5D;%0A        int count = 0;%0A        while (i
  %3C S
@@ -1173,23 +1173,16 @@
 h() && c
-hars%5Bi%5D
  == char
@@ -1188,19 +1188,88 @@
 rs%5Bi
- + 1%5D
+%5D) %7B%0A            i++;%0A            count++;%0A        %7D%0A%0A        if (count == 0
 ) %7B%0A
-//
     
@@ -1287,53 +1287,173 @@
 urn 
-%22%22;%0A//        %7D%0A        return sb.toString();
+sb.toString();%0A        %7D else if (count == 1) %7B%0A            sb.append(chars%5Bi%5D);%0A            return sb.toString();%0A        %7D else %7B%0A            return  %22%22;%0A        %7D
 %0A   
@@ -2064,16 +2064,18 @@
 / ababa%0A
+//
         
@@ -2135,18 +2135,16 @@
 / babab%0A
-//
         
@@ -2202,20 +2202,148 @@
 ; // ababab%0A
+//        System.out.println(prob.reorganizeString(%22a%22)); // a%0A//        System.out.println(prob.reorganizeString(%22ab%22)); // ab%0A
     %7D%0A%7D%0A

Update problem 767