본문 바로가기

백준

BOJ_4659 비밀번호 발음하기

백준 링크 : https://www.acmicpc.net/problem/4659

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
* 모음(a,e,i,o,u) 하나를 반드시 포함하여야 한다.
모음이 3개 혹은 자음이 3개 연속으로 오면 안 된다.
같은 글자가 연속적으로 두번 오면 안되나, ee 와 oo는 허용한다.
* */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;



        while(true) {
            st = new StringTokenizer(br.readLine());
            String pw = st.nextToken();
            if (pw.equals("end")) break;
            else {
                String vowels[] = {"a", "e", "i", "o", "u"};
                String consonant[] = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
                String arr[] = pw.split(""); // 비밀번호를 배열로 저장
                String checkTwo = "";
                boolean result = true;
                boolean checkMoum = false;

                for (String check : arr) {

                    if (!checkMoum) {
                        // 입력한 pw에 모음이 존재 할 경우 mo를 true로 변경
                        for (String mo : vowels) {
                            // 입력한 pw에 모음이 존재한다면 break;
                            if (check.equals(mo)) {
                                checkMoum = true;
                                result = true;
                                break;
                            } else {
                                // 존재하지 않는다면 false
                                result = false;
                            }
                        }
                    }

                    String regex = "[aeiou]{3,}|[^aeiou]{3,}";
                    Pattern pattern = Pattern.compile(regex);
                    Matcher matcher;
                    matcher = pattern.matcher(pw);
                    if(matcher.find()){
                        result = false;
                    }

                    // 같은 글자가 2번 나온다면
                    if (check.equals(checkTwo)) {
                        // 들어온 문자가 e나 o가 아니면 부적절한 pw
                        if (!check.equals("e") && !checkTwo.equals("e") && !check.equals("o") && !checkTwo.equals("o") ) {
                            result = false;
                        }
                    }
                    checkTwo = check;
                }
                if (result) {
                    System.out.println("<" + pw + ">" + " is acceptable.");
                } else {
                    System.out.println("<" + pw + ">" + " is not acceptable.");
                }
            }
        }
    }
}

 

TIP

1. 배열의 인덱스의 값을 비교할 때는 for-each문을 사용하지 않는 것이 좋다.

2. 해당 항목의 값 일치여부를 확인 할 때는 List의 contain 함수를 사용하는 것이 더 효율적이다.

'백준' 카테고리의 다른 글

BOJ_1515 수 이어쓰기  (0) 2023.01.24
BOJ_1446 지름길  (0) 2023.01.24
BOJ_1406 에디터  (0) 2023.01.16
BOJ_1158 요세푸스 문제  (0) 2023.01.16
BOJ_9655 돌 게임  (0) 2023.01.16