알고리즘 풀이

[JAVASCRIPT] 백준 4659번 문제 풀이

Lv1_junior_dev 2023. 5. 26. 20:31

 

❗ 풀이

const input = require('fs').readFileSync('dev/stdin').toString().trim().split(/\r?\n/);

const gather = ['a', 'e', 'i', 'o', 'u'];
const allowedStr = ['ee', 'oo'];

for (let i = 0; i < input.length - 1; i++) {
    const word = input[i];
    let vowelCount = 0;
    let consonantCount = 0;
    let isAcceptable = true;

    if (!gather.some(element => word.includes(element))) {
        console.log(`<${word}> is not acceptable.`);
        continue;
    }

    for (let j = 0; j < word.length; j++) {
        const currentChar = word[j];
        const nextChar = word[j + 1];

        if (currentChar === nextChar && !allowedStr.includes(currentChar + nextChar)) {
            console.log(`<${word}> is not acceptable.`);
            isAcceptable = false;
            break;
        }

        if (gather.includes(currentChar)) {
            consonantCount = 0;
            vowelCount++;
            if (vowelCount >= 3) {
                console.log(`<${word}> is not acceptable.`);
                isAcceptable = false;
                break;
            }
        } else {
            vowelCount = 0;
            consonantCount++;
            if (consonantCount >= 3) {
                console.log(`<${word}> is not acceptable.`);
                isAcceptable = false;
                break;
            }
        }
    }

    if (isAcceptable) {
        console.log(`<${word}> is acceptable.`);
    }
}

풀이라고 하기엔 사실 크게 뭐 없고...

  1. 모음(a,e,i,o,u) 하나를 반드시 포함하여야 한다.
  2. 모음이 3개 혹은 자음이 3개 연속으로 오면 안 된다.
  3. 같은 글자가 연속적으로 두번 오면 안되나, ee 와 oo는 허용한다.

해당 조건들만 잘 설정해주면 된다.

 

if문들 적재적소에 잘 사용해야하는데 그걸 제대로 못해버려서 꽤나 머리 아픈 문제였다.
정규표현식으로 풀면 더 멋지게 풀 수 있을거 같은데 난 못하겠다....