PS/백준
[백준] [JS] 10809_알파벳 찾기
JUNSANG YOO
2023. 9. 15. 14:45
문제
문제 출처 - https://www.acmicpc.net/problem/10809
10809번: 알파벳 찾기
각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출
www.acmicpc.net
풀이
소문자 a의 아스키코드는 97, z는 122이므로,
이 구간을 for문으로 돌린다.
문자열.indexOf(String.fromCharCode(아스키코드));
해당 아스키코드의 문자가 있는 위치를 indexOf 메소드로 찾아준다.
코드
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(filePath).toString().split('\n');
const str = input[0].split('');
let ans = [];
for (let i = 97; i <= 122; i++) {
ans.push(str.indexOf(String.fromCharCode(i)));
}
console.log(ans.join(' '));