코딩응급실
프로그래머스: 문자열 내림차순으로 배치하기 본문
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"
}
}
'Java' 카테고리의 다른 글
프로그래머스: 문자열 내 마음대로 정렬하기 (0) | 2024.03.02 |
---|---|
프로그래머스: 문자열 내 p와 y의 개수 (0) | 2024.03.02 |
프로그래머스: 서울에서 김서방 찾기 (0) | 2024.03.02 |
프로그래머스: 소수 찾기 (0) | 2024.03.02 |
프로그래머스: 수박수박수박수박수박수? (0) | 2024.03.02 |