๊ด€๋ฆฌ ๋ฉ”๋‰ด

\(@^0^@)/

[JS] ๋ฐฑ์ค€ 10809 ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[JS] ๋ฐฑ์ค€ 10809 ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ

minjuuu 2021. 10. 11. 17:19
728x90

๐Ÿฑ‍๐Ÿ‘“ 1. ๋ฌธ์ œ : 10809

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


๐Ÿ”ฅ 2. ์ฝ”๋“œ ๋ฐ ํ’€์ด

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(file).toString().split('\n');
let str = input[0];

const result = [];

// ์•„์Šคํ‚ค ์ฝ”๋“œ์—์„œ a-z๋Š” 97-122๊นŒ์ง€ 97-122์˜ ๋ฒ”์œ„๋ฅผ ๋Œ๋ฉด์„œ,
// String.fromCharCode => ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ a-z๊นŒ์ง€์˜ ์•ŒํŒŒ๋ฒณ์„ ์ƒ์„ฑ,
// str.indexOf => ๋ฐฐ์—ด์— ํ•ด๋‹น ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๊ทธ ๊ฐ’์˜ index๋ฅผ ๋„ฃ์ž (๋ฐฐ์—ด์— ํ•ด๋‹น ๊ฐ’์ด ์—†๋‹ค๋ฉด, -1์ด ์ €์ ˆ๋กœ ์ถœ๋ ฅ๋จ)
// result.push => push ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ result ๋ฐฐ์—ด์— ์ถ”๊ฐ€.

for (let i = 97; i <= 122; i++) {
  result.push(str.indexOf(String.fromCharCode(i)))
}

// join์œผ๋กœ ๊ณต๋ฐฑ์„ ์ค˜์„œ, ์ถœ๋ ฅ ํ˜•์‹ ๋งž์ถ”๊ธฐ
console.log(result.join(" "));
728x90