백준-4659번 비밀번호 발음하기

Titanium·2025년 1월 10일


package b4659;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');

        while (true) {
            String password = sc.nextLine();

            if (password.equals("end")) {
                break;
            }

            if (isValidPassword(password, vowels)) {
                System.out.println("<" + password + "> is acceptable.");
            } else {
                System.out.println("<" + password + "> is not acceptable.");
            }
        }

        sc.close();
    }

    public static boolean isValidPassword(String password, List<Character> vowels) {
        boolean hasVowel = false;
        int consecutiveVowels = 0;
        int consecutiveConsonants = 0;
        char prevChar = '\0';

        for (int i = 0; i < password.length(); i++) {
            char currentChar = password.charAt(i);

            if (vowels.contains(currentChar)) {
                hasVowel = true;
                consecutiveVowels++;
                consecutiveConsonants = 0;
            } else {
                consecutiveVowels = 0;
                consecutiveConsonants++;
            }

            if (consecutiveVowels >= 3 || consecutiveConsonants >= 3) {
                return false;
            }

            if (currentChar == prevChar && currentChar != 'e' && currentChar != 'o') {
                return false;
            }

            prevChar = currentChar;
        }

        return hasVowel;
    }
}

profile
핵심먼저

0개의 댓글