[코테 풀이] Sorting the Sentence

시내·2024년 6월 15일
0

Q_1859) Sorting the Sentence

출처 : https://leetcode.com/problems/sorting-the-sentence/

class Solution {
    
    public class OrderCompare implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            return o1.charAt(o1.length() - 1) - o2.charAt(o2.length() - 1);
        }
    }
    
    public String sortSentence(String s) {
        String answer = "";
        String[] split = s.split(" ");
        ArrayList<String> arrayList = new ArrayList<>();
        for (String str : split) arrayList.add(str);
        Collections.sort(arrayList, new OrderCompare());
        for (int a = 0; a < arrayList.size(); a++) {
            for (int b = 0; b < arrayList.get(a).length(); b++) {
                if (Character.isDigit(arrayList.get(a).charAt(b))) answer += " ";
                else answer += arrayList.get(a).charAt(b);
            }
        }
        return answer.substring(0,answer.length()-1);
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글