본문 바로가기

프로그래머스/lv0

A로 B 만들기

/* A로 B 만들기
 * 문자열 before와 after가 매개변수로 주어질 때,
 * before의 순서를 바꾸어 after를 만들 수 있으면 1을,
 * 만들 수 없으면 0을 return
 *
 * before   after   result
 * "olleh"  "hello" 1
 * "allpe"  "apple" 0
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;
public class programmer_0_32 {
    static String a1 = "olleh";    static String b1 = "hello";    
    static String a2 = "allpe";    static String b2 = "apple";   
    public int solution(String before, String after) {
        int answer = 0;
 
        String[] a = before.split("");
        String[] b = after.split("");
        Arrays.sort(a);
        Arrays.sort(b);
 
        return Arrays.toString(a).equals(Arrays.toString(b)) ? 1 :0;
    }
    public static void main(String args[]){
        programmer_0_32 t = new programmer_0_32();
 
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1,b1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2,b2));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

중복된 문자 제거  (0) 2022.12.05
k의 개수  (0) 2022.12.05
최빈값 구하기  (1) 2022.12.05
두 수의 나눗셈  (0) 2022.12.05
숫자 비교하기  (0) 2022.12.05