[백준] 4659 : 비밀번호 발음하기

ByWindow·2021년 5월 1일
0

Algorithm

목록 보기
25/104
post-thumbnail

📝 문제

세 가지 조건을 최소한의 비용으로 검사하는 방법이 무엇일까를 고민했다.
하지만 그런 방법은 없었고(내가 떠올리지 못한 걸 수도...) 그래서 사용하는 변수가 많아졌다.

📌 코드

package Baekjoon;

import java.util.*;
import java.io.*;

public class BOJ4659 {
    public static void main(String[] args) throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        char[] vowels = { 'a', 'e', 'i', 'o', 'u'}; //모음

        while(true){
            st = new StringTokenizer(br.readLine());
            String input = st.nextToken();
            if(input.equals("end")) break;
            else {
                boolean acceptable = true;
                boolean hasVowel = false;
                int cntVowel = 0;//모음 카운트
                int cntConsonant = 0;//자음 카운트
                for(int i = 0; i < input.length(); i++){
                    char cur = input.charAt(i);
                    boolean isVoewl = false;
                    //모음인지 확인 : 모음이 연속 몇 번 나오는지 체크, 자음 연속 개수는 0으로
                    for(int j = 0; j < vowels.length; j++){
                        if(cur == vowels[j]){
                            isVoewl = true;
                            hasVowel = true;
                            cntVowel++;
                            cntConsonant = 0;
                            break;
                        }
                    }
                    //자음이라면
                    if(!isVoewl) {
                        cntConsonant++;
                        cntVowel = 0;
                    }
                    //문자열 끝까지 탐색했는데 모음이 없다면
                    if(i == input.length()-1){
                        if(!hasVowel) {
                            System.out.println("<" + input + "> is not acceptable.");
                            acceptable = false;
                            break;
                        }
                    }
                    if(i >= 1){
                        //동일한 문자가 2개 연속되는지 검사
                        if(cur == input.charAt(i-1) && cur != 'e' && cur != 'o'){
                            System.out.println("<" + input + "> is not acceptable.");
                            acceptable = false;
                            break;
                        }
                        //모음 혹은 자음이 3개 연속되는지 검사
                        else if(cntVowel >= 3 || cntConsonant >= 3){
                            System.out.println("<" + input + "> is not acceptable.");
                            acceptable = false;
                            break;
                        }
                    }
                }
                if(acceptable) System.out.println("<" + input + "> is acceptable.");
            }
        }
    }
}
profile
step by step...my devlog

0개의 댓글