Algorithm/Baekjoon

[Algorithm/Baekjoon] ๊ณ ๋ƒฅ์ด - 16472(G4/JAVA)

dpdms2148 2024. 12. 5. 21:07
728x90

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

https://www.acmicpc.net/problem/16472

๐Ÿ’ป์ฝ”๋“œ

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());        // ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ ์ข…๋ฅ˜ ์ตœ๋Œ€ ๊ฐฏ์ˆ˜
        char[] catString = br.readLine().toCharArray(); // ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด
        int[] alphabet = new int[26];
        int answer = 0;
        int count = 0;

        for (int start = 0, end = 0; end < catString.length; end++) {
            // ์ฒ˜์Œ ์ธ์‹ํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์ด๋ผ๋ฉด count ์ฆ๊ฐ€
            if (alphabet[catString[end] - 'a']++ == 0) count++;

            // count๊ฐ€ N๋ณด๋‹ค ํฌ๋ฉด ์ค„์–ด๋“ค ๋•Œ ๊นŒ์ง€ start๋ฅผ ์ด๋™
            while (N < count&& start < end) {
                if (--alphabet[catString[start++] - 'a'] == 0) count--;
            }
            //์ตœ๋Œ“๊ฐ’ ์ €์žฅ
            answer = Math.max(answer, end - start + 1);
        }

        System.out.println(answer);
    }
}
728x90