๐ ์๊ณ ๋ฆฌ์ฆ/๋ฐฑ์ค JS
[๋ฐฑ์ค] JS/ 10808 ์ํ๋ฒณ ๊ฐ์
Tamii
2021. 9. 30. 09:46
๋ฐ์ํ
๋ด ํ์ด
const fs = require('fs');
let input = (fs.readFileSync('./test') + '').toString().trim().split('');
let ans = new Array(26).fill(0);
input.map((alpha) => {
let alpha2Ascii = alpha.charCodeAt(0);
ans[alpha2Ascii - 97] += 1;
});
console.log(ans.join(' '));
์ ๋ง ๋จ์ํ
๋ฐ์ ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋๋ฉฐ Ascii๋ก ๋ฐ๊ฟ์ฃผ๊ณ ํด๋น ๋ฐฐ์ด์ idx-97 (a = 97)์ ๋ฃ์ด์ฃผ๊ธฐ!
๋ค๋ฅธ ํ์ด
const s = require("fs").readFileSync("/dev/stdin").toString().split("");
const alphabet = "abcdefghijklmnopqrstuvwxyz";
const counts = new Array(26).fill(0);
s.forEach(i => counts[alphabet.indexOf(i)]++);
console.log(counts.join(" "));
ํธ์ค......
๋ฌธ์์ด์ ์ธ๋ฑ์ค๋ฅผ ๋ฃ์ด์ฃผ๋ ๋ฐฉ๋ฒ์ ์๊ฐ์ ๋ชปํ๋ค. ๊ตณ์ด Ascii๋ก ๋ณํํ ํ์๊ฐ ์์๋ ๋ชจ์์ ๋๋ค.