Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

프로그래머스: 문자열 내림차순으로 배치하기 본문

Java

프로그래머스: 문자열 내림차순으로 배치하기

Daeryuk Kim 2024. 3. 2. 18:37
import java.util.*;

class Solution {
    public String solution(String s) {
        char[] chars = s.toCharArray();
        Arrays.sort(chars); // 문자 배열 정렬

        // 배열을 역순으로 재배치
        StringBuilder sb = new StringBuilder(new String(chars)).reverse();
        return sb.toString();
    }

    public static void main(String[] args) {
        String s = "Zbcdefg";
        Solution sol = new Solution();
        String result = sol.solution(s);
        System.out.print(result); // 출력: "gfedcbZ"
    }
}