문제 🔽
https://school.programmers.co.kr/learn/courses/30/lessons/142086
다른사람 풀이 🔽
const solution = (s) =>
[...s].map((char, i) => {
const count = s.slice(0, i).lastIndexOf(char);
return count < 0 ? count : i - count;
});
내풀이🔽
1. 성공 ⭕
function solution(s) {
var answer = Array.from(s,()=>{return -1})
for(let i=s.length-1; i>=0; --i){
let adjacentIndex=i-1;
while(adjacentIndex>=0){
if(s[adjacentIndex]==s[i]){
answer[i]=i-adjacentIndex
break;
}else{
--adjacentIndex;
}
}
}
return answer;
}
느낀점🔽
이중 for문 돌리기 보단 안돌리는 방향으로 진행하자
그리고 몰랐는데 lastIndexOf() 라는 메서드가 있다 이를 활용해보자
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
'Javascript' 카테고리의 다른 글
프로그래머스 코딩테스트 풀이(js) > 모의고사(lv1) (0) | 2023.07.19 |
---|---|
프로그래머스 코딩테스트 풀이(js) > 카드뭉치(lv1) (0) | 2023.07.18 |
프로그래머스 코딩테스트 풀이(js) > 피로도 (0) | 2023.07.13 |
프로그래머스 코딩테스트 풀이(js) > 할인 행사 (0) | 2023.07.10 |
프로그래머스 코딩테스트 풀이(js) > 기능개발 (0) | 2023.07.09 |