풀이
- 알파벳 개수만큼 담긴 배열을 만들고, -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));
'알고리즘' 카테고리의 다른 글
| 프로그래머스 - 주식가격 (Javascript) 풀이 (0) | 2024.06.16 |
|---|---|
| 프로그래머스 - 다리를 지나는 트럭 (Javascript) 풀이 (0) | 2024.06.15 |
| 프로그래머스 - 프로세스 (Javascript) 풀이 (0) | 2024.06.12 |
| 프로그래머스 - 기능개발 (Javascript) 풀이 (1) | 2024.06.09 |
| 프로그래머스 - 같은 숫자는 싫어 (Javascript) 풀이 (0) | 2024.06.08 |