๋ฌธ์ ๐ฝ
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
'๐Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ํ์ด(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 |