728x90
반응형
solution("banana")
if 이후-> overlap : ['b','a','n']
else{
[1] : overlap.length=3 overlap.lastIndexOf(s[i])=1 -> overlap.push(s[i]) = 'a'
overlap : ['b','a','n','a']
[2] : overlap.length=4 overlap.lastIndexOf(s[i])=2
[3] : overlap.length=5 overlap.lastIndexOf(s[i])=3
lastIndexOf를 사용함으로써 가장 위쪽의 있는 글자와 비교를 하기 위해서
function solution(s) {
let overlap=[]
let res=[]
for (const i in s) {
if(!overlap.includes(s[i])){
overlap.push(s[i])
res.push(-1)
}
else{
res.push(overlap.length-overlap.lastIndexOf(s[i]))
overlap.push(s[i])
}
}
return res
}
728x90
반응형
'알고리즘(Algorithm)' 카테고리의 다른 글
프로그래머스 - 크기가 작은 부분문자열[JS] (0) | 2022.12.23 |
---|---|
프로그래머스 - 문자열 나누기[JS] (0) | 2022.12.19 |
프로그래머스 시저 암호 [JS] (0) | 2022.12.17 |
unsigned int (0) | 2022.10.04 |
그래프 순회 (0) | 2022.09.10 |