[백준 2941번/node.js] 크로아티아 알파벳

김겜김·2024년 1월 17일
0
post-thumbnail

🎈문제


🎲입출력


출력값

[Running] node "c:\Users\TED\Desktop\백준알고리즘\test\6단계\n2941.js"
6

[Done] exited with code=0 in 0.109 seconds

문제해결방법
이번 문제에서는 베열에 크로아티아 알파벳을 넣고 반복문을 돌리면 쉽게 해결할거라 생각했지만 역시나 쉽게 문제는 생각나지 않았습니다 ㅠㅠㅠㅠ

찾아본 결과 함수를 이용하여 푸는 방법이 눈에 들어왔고 반복문안에 배열을 split()하여 크로아티아 알파벳이 몇개가 포함되어었는지 쉽게 찾을수있었습니다.

🗂️코드


const fs = require('fs');

try {
  const countCroatianAlphabet = (word) => {
    const croatianAlphabets = ['c=', 'c-', 'dz=', 'd-', 'lj', 'nj', 's=', 'z='];

    for (const alphabet of croatianAlphabets) {
      word = word.split(alphabet).join('1');
    }

    return word.length;
  };

  const main = () => {
    //문제풀이용
    //const input = fs.readFileSync('answer/level6/n2941.txt').toString().trim();

    //백준 제출용
    const input = fs.readFileSync('dev/stdin').toString();

    const result = countCroatianAlphabet(input);
    console.log(result);
  };

  main();
} catch (err) {
  console.log(err.message);
}
profile
개발에 관심이있습니다

0개의 댓글