[BOJ 2607] ๋น„์Šทํ•œ ๋‹จ์–ด

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/2607

 

2. ํ’€์ด

์•ŒํŒŒ๋ฒณ์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ  char์„ ์ธ๋ฑ์Šค๋กœ ๋ฐ”๊ฟ”์„œ cnt ๊ณ„์‚ฐ ํ›„ ๋น„๊ต

 

3. ์ฝ”๋“œ

package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ๋น„์Šทํ•œ๋‹จ์–ด {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        String first = br.readLine();
        int result = 0;

        for(int i = 0; i < N - 1; i++) {
            String str = br.readLine();
            int cnt = 0;    // ๊ฐ™์€ ๋‹จ์–ด ๊ฐœ์ˆ˜
            int[] word = new int[26];
            for(int j = 0; j < first.length(); j++) {
                word[first.charAt(j) - 'A']++;
            }

            for(int j = 0; j < str.length(); j++) {
                if(word[str.charAt(j) - 'A'] > 0) {
                    cnt++;
                    word[str.charAt(j) - 'A']--;
                }
            }
            // ๊ธฐ์ค€ ๋ฌธ์ž์—ด ๊ธธ์ด์™€ ๋น„๊ต ๋ฌธ์ž์—ด ๊ธธ์ด ์ฐจ์ด
            // ๊ฐ™์€ ๊ฒฝ์šฐ or ํ•œ ๊ธ€์ž๋งŒ ๋ฐ”๋€๊ฒฝ์šฐ
            if(first.length() == str.length() && (first.length() == cnt || first.length() - 1 == cnt)) {
                result++;
            }
            // ๋น„๊ต ๋ฌธ์ž์—ด์ด ์งง์€ ๊ฒฝ์šฐ (์ œ๊ฑฐ)
            else if (first.length() == str.length() - 1 && str.length() - 1 == cnt) {
                result++;
            }
            // ๋น„๊ต ๋ฌธ์ž์—ด์ด ๊ธด ๊ฒฝ์šฐ (์ถ”๊ฐ€)
            else if (first.length() == str.length() + 1 && str.length() == cnt) {
                result++;
            }

        }
        System.out.println(result);

    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ