출처 : https://leetcode.com/problems/increasing-decreasing-string/
You are given a string s. Reorder the string using the following algorithm:
s and append it to the result.s which is greater than the last appended character to the result and append it.s and append it to the result.s which is smaller than the last appended character to the result and append it.s.In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.
Return the result string after sorting s with this algorithm.

class Solution {
public class Alphabet {
char letter;
int count;
public Alphabet(char letter, int count) {
this.letter = letter;
this.count = count;
}
}
public class AlphabeticalOrder implements Comparator<Alphabet> {
@Override
public int compare(Alphabet o1, Alphabet o2) {
return o1.letter - o2.letter;
}
}
public class ReverseOrder implements Comparator<Alphabet> {
@Override
public int compare(Alphabet o1, Alphabet o2) {
return o2.letter - o1.letter;
}
}
public String sortString(String s) {
String result = "";
ArrayList<Alphabet> alphabetArrayList = new ArrayList<>();
Map<Character, Integer> map = new HashMap<>();
for (int a = 0; a < s.length(); a++) {
if (!map.containsKey(s.charAt(a))) map.put(s.charAt(a), 1);
else map.put(s.charAt(a), map.get(s.charAt(a)) + 1);
}
Iterator<Character> it = map.keySet().iterator();
while (it.hasNext()) {
Character next = it.next();
alphabetArrayList.add(new Alphabet(next, map.get(next)));
}
int count = 0;
while (count < s.length()) {
for (int i = 0; i < alphabetArrayList.size(); i++) {
Collections.sort(alphabetArrayList, new AlphabeticalOrder());
if (alphabetArrayList.get(i).count > 0) {
result += alphabetArrayList.get(i).letter;
alphabetArrayList.get(i).count--;
count++;
}
}
for (int j = 0; j < alphabetArrayList.size(); j++) {
Collections.sort(alphabetArrayList, new ReverseOrder());
if (alphabetArrayList.get(j).count > 0) {
result += alphabetArrayList.get(j).letter;
alphabetArrayList.get(j).count--;
count++;
}
}
}
return result;
}
}