유물/└ 백준

<백준> 10809번: 알파벳 찾기

디벅잉 2022. 1. 20. 20:21
728x90

 

🤖

 

문제

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

 

10809번: 알파벳 찾기

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출

www.acmicpc.net

 

풀이

const fs = require("fs");
const S = fs.readFileSync("/dev/stdin").toString().trim();

let result = "";

for (let i = 97; i <= 122; i++) {
  let numToAdd = "-1";
  for (const s of S) {
    if (String.fromCharCode(i) === s) {
      numToAdd = S.indexOf(s);
      break;
    }
  }
  result = result + " " + numToAdd;
}

console.log(result.trim());

1. (line 9) fromCharCode 메서드를 활용하여 아스키코드에 대응하는 알파벳을 확인합니다.

2. (line 17) "출력 형식이 잘못되었습니다" 결과를 고치기 위해 trim() 처리하였습니다.

 

728x90