๊ธฐ๋ก๋ฐฉ

BOJ_6996 : ์• ๋„ˆ๊ทธ๋žจ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_6996 : ์• ๋„ˆ๊ทธ๋žจ

Soom_1n 2022. 10. 24. 15:34

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

6996๋ฒˆ: ์• ๋„ˆ๊ทธ๋žจ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜(<100)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ๊ธธ์ด๊ฐ€ 100์„ ๋„˜์ง€ ์•Š๋Š” ๋‹จ์–ด๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์„œ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋ฐ›์€ ๋‘ ๋ฌธ์ž์—ด์ด ์• ๋„ˆ๊ทธ๋žจ์ธ์ง€ ํŒ๋‹จํ•˜๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.
    • ์• ๋„ˆ๊ทธ๋žจ์€ ๋ฌธ์ž์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด์ด๋‹ค.
    • ๋ฐ”๊พธ์ง€ ์•Š์€ ๋™์ผํ•œ ๋ฌธ์ž์—ด๋„ ์• ๋„ˆ๊ทธ๋žจ์ด๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            String as = st.nextToken();
            String bs = st.nextToken();
            char a[] = as.toCharArray();
            char b[] = bs.toCharArray();
            Arrays.sort(a);
            Arrays.sort(b);
            String aa = new String(a);
            String bb = new String(b);

            if (aa.compareTo(bb) == 0){
                sb.append(as +" & "+ bs +" are anagrams.\n");
            }
            else {
                sb.append(as +" & "+ bs +" are NOT anagrams.\n");
            }
        }
        System.out.println(sb);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋‘ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ์ •๋ ฌ ํ›„ ๋น„๊ตํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • ๋ฌธ์ž์—ด์ธ Stringํ˜•์„ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด charํ˜• ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ ํ•œ๋‹ค.
    • Arrays.sort()๋ฅผ ์ด์šฉํ•ด charํ˜• ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
    • ์ •๋ ฌ๋œ charํ˜• ๋ฐฐ์—ด์„ ๋‹ค์‹œ String ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
    • ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ compareTo()๋กœ ๋น„๊ตํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • python๊ฐ™์ด in ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์–ด์„œ ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ• ๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€, ์ •๋ ฌ๋กœ ๋น„๊ต์  ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฑธ ์•Œ์•„์ฑ˜๋‹ค.
  • ์ด์ „์— C++๊ณต๋ถ€ํ• ๋•Œ ํ‹€๋ฆฐ์ด์œ ๋Š” ๋‘ ๋ฌธ์ž์—ด ๋น„๊ต๋ฅผ '=='๋กœ ๋น„๊ตํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • String์„ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ ์ •๋ฆฌ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ–ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_2003 : ์ˆ˜๋“ค์˜ ํ•ฉ 2  (0) 2022.10.26
BOJ_11004 : K๋ฒˆ์žฌ ์ˆ˜  (0) 2022.10.25
BOJ_10989 : ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ 3  (0) 2022.10.23
BOJ_1411 : ๋น„์Šทํ•œ ๋‹จ์–ด  (0) 2022.10.20
BOJ_1049 : ๊ธฐํƒ€์ค„  (0) 2022.10.19