[코테 풀이] Increasing Decreasing String

시내·2024년 6월 28일

Q_1370) Increasing Decreasing String

출처 : https://leetcode.com/problems/increasing-decreasing-string/

You are given a string s. Reorder the string using the following algorithm:

  1. Pick the smallest character from s and append it to the result.
  2. Pick the smallest character from s which is greater than the last appended character to the result and append it.
  3. Repeat step 2 until you cannot pick more characters.
  4. Pick the largest character from s and append it to the result.
  5. Pick the largest character from s which is smaller than the last appended character to the result and append it.
  6. Repeat step 5 until you cannot pick more characters.
  7. Repeat the steps from 1 to 6 until you pick all characters from 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;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글