코딩테스트/자바 문제풀이

[CLASS 3: 문자열] 백준 5525 IOIOI

승요나라 2024. 11. 17. 20:42

5525번: IOIOI

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

 

# 코드

import java.io.*;

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());
        int m = Integer.parseInt(br.readLine());
        String str = br.readLine();
        int cnt = 0, ans = 0;
        for(int i=1; i<m-1; ) {
            if(str.charAt(i) == 'O' && str.charAt(i+1) == 'I') {
                cnt++;
                if(cnt == n) {
                    if(str.charAt(i-(cnt*2-1)) == 'I')
                        ans++;
                    cnt--;
                }
                i += 2;
            }
            else {
                cnt = 0;
                i++;
            }
        }
        System.out.println(ans);
    }
}