본문 바로가기
개발 노트/백준, 프로그래머스 풀이

[백준 16499/javascript] 동일한 단어 그룹화하기

by tokkiC 2022. 8. 30.

문자열의 각 문자를 배열에 넣고 정렬하여

Set 객체에 넣어 중복을 제거하여 Set 객체 내의 요소 수를 세주면 되는 문제이다

문제보다 헛짓으로 인한 부수적으로 얻은 지식이 큰 도움이 된 문제이다

https://www.acmicpc.net/problem/16499

 

16499번: 동일한 단어 그룹화하기

첫째 줄에 단어의 개수 N이 주어진다. (2 ≤ N ≤ 100) 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 소문자로만 이루어져 있고, 길이는 10을 넘지 않는다.

www.acmicpc.net

let input = [];

const readline = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readline.on("line", (line) => {
  input.push(line);
});

readline.on("close", () => {
  solution(input);
  process.exit();
});

const solution = (inp) => {
  inp.shift();
  let inparr = [];
  inp.forEach((el) => {
    let temp = [];
    for (ch of el) {
      temp.push(ch);
      temp.sort();
    }
    inparr.push(temp.join(""));
  });
  let set = new Set(inparr);
  console.log(set.size);
};

댓글