[백준1157_자바스크립트(javascript)] - 단어 공부

경이·2024년 8월 23일

𝑩𝑶𝑱 (𝒋𝒔)

목록 보기
154/325

🔴 문제

단어 공부


🟡 Sol

const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : 'Wiki\\input.txt';
const input = fs.readFileSync(path).toString().trim().toUpperCase();
const wordSet = new Set(input);
const wordCnt = [];

for (const w of wordSet) {
  const wCnt = input.split(w).length - 1;
  wordCnt.push([w, wCnt]);
}
const sortedWordCnt = wordCnt.sort((a, b) => b[1] - a[1]);

if (sortedWordCnt.length === 1 || sortedWordCnt[0][1] !== sortedWordCnt[1][1]) {
  console.log(sortedWordCnt[0][0]);
} else {
  console.log('?');
}

🟢 풀이

⏰ 소요한 시간 : -

단어를 모두 대문자로 변경해준뒤 Set을 사용해 중복을 제거한 wordSet을 만든다.
그 후 wordSet을 순회하면서 w가 몇번 나왔는지 세서 아래처럼 배열형태로 저장한다.

wordCnt = [['M', 1], ['S', 4]]

배열을 정렬해준 뒤 가장 많이 사용된 알파벳이 여러갠지 아닌지에 따라 결과를 출력하면 된다.


🔵 Ref

profile
록타르오가르

0개의 댓글