알고리즘

백준 - 알파벳 찾기 10809번 - JS

jaewoo 2025. 11. 2. 10:20

 

 

 

풀이

 

- 알파벳 개수만큼 담긴 배열을 만들고, -1을 모두 삽입합니다.

- 소문자 a (아스킷 97) 을 기준으로 알파벳 인덱스를 찾습니다.

- inputWord 반복 조회(문자열은 배열이기에 length 및 index로 특정 문자 접근 가능함, 안전하게 하고 싶으면 Array.from() 으로 처리하면됩니다.)

- 알파벳 배열에 -1 이 들어가 있으면 아직 한번도 조회가 안되었던 데이터이기에 해당 index에 문자열 배열 인덱스를 넣어줍니다. 

 

 

const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");


const S = input[0];

function solution(inputWord) {
  const alphabetArray = new Array(26).fill(-1);


  for (let index = 0; index < inputWord.length; index++) {
    const s = inputWord[index]; // 현재 인덱스의 문자를 가져옵니다.

    const ascCode = s.charCodeAt(0);
    const findIndex = ascCode - 97;

    //  -1일 때만(처음 발견) 인덱스를 기록
    if (alphabetArray[findIndex] === -1) {
      alphabetArray[findIndex] = index;
    }
  }

  return alphabetArray.join(" ");
}

console.log(solution(S));