문제
한 개의 문자열 s와 문자 t가 주어지면 문자열 s의 각 문자가 문자 t와 떨어진 최소거리를 출력
풀이 과정
const solutionA = (word, text) => {
let answer = [];
let count = 0;
for (let chaaracter of word) {
if (chaaracter === text) count = 0;
else count++;
answer.push(count);
}
count = 0;
for (let i = word.length - 1; i >= 0; i--) {
const element = word[i];
if (element === text) count = 0;
else count++;
answer[i] = Math.min(answer[i], count);
}
return answer;
};
const testA = solutionA('teachermode', 'e');
console.log(testA);
틀린 부분이 있거나 보충해야 할 내용이 있다면 댓글이나 DM(sungstonemin)으로 알려주시면 감사하겠습니다😄