알고리즘(Algorithm)
프로그래머스 - 가장 가까운 글자 [JS]
Hun-bot
2022. 12. 18. 23:06
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
반응형