사랑하애오

솔루션1

const convertPWordsToNum = (word) => {
  const pWords = ["aya", "ye", "woo", "ma"];
  return pWords.reduce((result, pWord, i) => result.replaceAll(pWord, i), word);
};

const canPronounce = (word) => {
  const result = convertPWordsToNum(word);
  return (
    !/[^\d]/.test(result) &&
    [...result].every(
      (num, i) => i + 1 > result.length || num !== result[i + 1]
    )
  );
};

function solution(babbling) {
  console.log(babbling.filter((b) => canPronounce(b)).length);
  return babbling.filter((b) => canPronounce(b)).length;
}

reduce와 정규식을 이용해서 풀이

 

 

 

솔루션2

const solution = (babbling) => {
  const can = ["aya", "ye", "woo", "ma"];

  let count = 0;
  for (let i = 0; i < babbling.length; i++) {
    let babble = babbling[i];

    for (let j = 0; j < can.length; j++) {
      if (babble.includes(can[j].repeat(2))) {
        break;
      }

      babble = babble.split(can[j]).join(" ");
    }

    if (babble.split(" ").join("").length === 0) {
      count += 1;
    }
  }

  console.log(count);
  return count;
};

이중for문으로 풀이

파이썬이나 자바스크립트처럼 인터프리터, 스크립트 언어는 이중for문이 안좋다고 한다.

 

 

 

솔루션3

const solution = (babbling) => {
  let answer = 0;
  const regex = /^(aya|ye|woo|ma)+$/; // 정규식

  babbling.forEach((word) => {
    if (regex.test(word)) {
      answer++;
    }
  });

  console.log(answer);
  return answer;
};

정규식으로 풀이

profile

사랑하애오

@사랑하애

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!