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++]);
            }
        }
        char c = chars[i++];
        int count = 0;
        while (i < S.length() && c == chars[i]) {
            i++;
            count++;
        }

        if (count == 0) {
            return sb.toString();
        } else if (count == 1) {
            sb.append(chars[i]);
            return sb.toString();
        } else {
            return  "";
        }
    }

    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
//        System.out.println(prob.reorganizeString("a")); // a
//        System.out.println(prob.reorganizeString("ab")); // ab
    }
}

@@ -167,24 +167,87 @@
 String S) %7B%0A
+        if (S.length() == 1) %7B%0A            return S;%0A        %7D%0A
         Char
@@ -1142,32 +1142,90 @@
     %7D%0A        %7D%0A
+        if (i - 1 %3C 0) %7B%0A            return %22%22;%0A        %7D%0A
         char c =
@@ -1232,18 +1232,20 @@
  chars%5Bi
-++
+ - 1
 %5D;%0A     
@@ -1477,23 +1477,16 @@
 append(c
-hars%5Bi%5D
 );%0A     
@@ -1780,113 +1780,8 @@
    %7D
-%0A%0A        @Override%0A        public String toString() %7B%0A            return ch + %22 --%3E %22 + count;%0A        %7D
 %0A   
@@ -2146,16 +2146,18 @@
 / babab%0A
+//
         
@@ -2347,16 +2347,148 @@
 ; // ab%0A
+//        System.out.println(prob.reorganizeString(%22aaa%22)); //%0A        System.out.println(prob.reorganizeString(%22bfrbs%22)); // brbsf%0A
     %7D%0A%7D%0A

Update problem 767