작성
·
298
0
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s, t) {
let sArr = s.split("");
let answer = sArr.slice();
sArr.forEach((element, index) => {
if (element == t) {
let p = 0;
answer[index] = p;
for (let i = index - 1; i >= 0; i--) {
p++;
if (typeof answer[i] === "string") answer[i] = p;
else answer[i] = Math.min(answer[i], p);
}
p = 0;
for (let i = index + 1; i < answer.length; i++) {
p++;
if (typeof answer[i] === "string") answer[i] = p;
else answer[i] = Math.min(answer[i], p);
}
}
});
return answer;
}
let str = "teachermode";
console.log(solution(str, "e"));
</script>
</body>
</html>