A로 B 만들기

han.user();·2023년 4월 2일
0

프로그래머스

목록 보기
28/87
post-thumbnail

import java.util.Arrays;

class Solution {
    public int solution(String before, String after) {
        // 입력된 문자열을 char 배열로 변환합니다.
        char[] beforeChars = before.toCharArray();
        char[] afterChars = after.toCharArray();
        
        // char 배열을 알파벳 순서대로 정렬합니다.
        Arrays.sort(beforeChars);
        Arrays.sort(afterChars);
        
        // 정렬된 char 배열을 다시 문자열로 변환합니다.
        String sortedBefore = new String(beforeChars);
        String sortedAfter = new String(afterChars);
        
        // 정렬된 before와 after 문자열이 같은지 확인합니다.
        if (sortedBefore.equals(sortedAfter)) {
            // 같다면 1을 반환합니다.
            return 1;
        } else {
            // 다르다면 0을 반환합니다.
            return 0;
        }
    }
}
profile
I'm still hungry.

0개의 댓글