본문 바로가기
Problem Solving/Baekjoon

[백준] 12933 오리 - Greedy / Java

by graycode 2023. 4. 14.

 문제 링크

 

12933번: 오리

첫째 줄에 영선이가 녹음한 소리가 주어진다. 소리의 길이는 5보다 크거나 같고, 2500보다 작거나 같은 자연수이고, 'q','u','a','c','k'로만 이루어져 있다.

www.acmicpc.net

 

 풀이 과정

 

 풀이 코드

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String input = br.readLine();

        bw.write(String.valueOf(getCount(input)));
        bw.flush();
    }

    private static int getCount(String input) {
        if (input.length() % 5 != 0)
            return -1;

        String quack = " quack";

        int[] cnt = new int[6];
        cnt[0] = input.length();

        int max = 0;
        for (char c : input.toCharArray()) {
            for (int i = 1; i <= 5; i++) {
                if (c == quack.charAt(i)) {
                    if (cnt[i - 1] == 0)
                        return -1;

                    cnt[i]++;
                    cnt[i - 1]--;
                }
            }
            max = Math.max(max, cnt[1] + cnt[2] + cnt[3] + cnt[4]);
        }

        return cnt[5] * 5 == input.length() ? max : -1;
    }

}

댓글