Inflearn Community Q&A
while문을 이용해서 풀었는데 한번 봐주세요
Written on
·
168
0
function solution(str, t) {
let answer = [];
let q = [];
for (let i = 0; i < str.length; i++) {
if (str[i] === t) {
answer[i] = 0;
continue;
}
q.push([str[i], 1]);
while (q.length > 0) {
const [c, num] = q.shift();
const left = i - num;
const right = i + num;
if (c === undefined) continue;
if (c !== t) {
q.push([str[left], num + 1], [str[right], num + 1]);
}
if (c === t) {
answer[i] = num - 1;
q = [];
}
}
}
console.log(answer);
}
요소의 왼쪽, 오른쪽 값들을 q에 넣어 bfs 형식으로 풀어봤는데 괜찮을까요?
javascript코테 준비 같이 해요!
Answer 1
0
codingcamp
Instructor
안녕하세요^^
타겟문자가 아닌 지점에서 좌우로 퍼져나가는 방법인데 for문안에 while 문이 있는 것 시간복잡도가 동일한 구조로 보여집니다.
영상에 방법도 익혀두시면 좋겠습니다.





