문제
https://www.acmicpc.net/problem/10808
10808번: 알파벳 개수
단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.
www.acmicpc.net
코드
//브론즈2 알파벳 개수 const fs = require("fs"); const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt"; let input = fs.readFileSync(filePath).toString().trim().split("\r\n"); //각 문자열과 a의 아스키코드 차이 수를 return하는 함수 function getASCIInum(str) { return str.charCodeAt() - "a".charCodeAt(); } const str = input.shift(); let alphabet = new Array(26).fill(0); //a-z 까지 담을 배열 생성 for (let i = 0; i < str.length; i++) { let index = getASCIInum(str[i]); //각 문자열과 a의 아스키 코드 차이를 통해 인덱스를 얻음 alphabet[index]++; //해당 인덱스에 +1 } console.log(alphabet.join(" "));
'알고리즘 > BOJ' 카테고리의 다른 글
[백준] 국영수 / JavaScript / node.js (0) | 2022.02.03 |
---|---|
[백준] K번째 수 / JavaScript / node.js (0) | 2022.02.03 |
[백준] 먹을 것인가 먹힐 것인가 / JavaScript / node.js (0) | 2022.02.01 |
[백준] 유기농 배추 / JavaScript / node.js (0) | 2022.01.31 |
[백준] DFS와 BFS / JavaScript / node.js (0) | 2022.01.30 |