Given a string s and a letter t, write a program that prints the minimum distance each letter of string s is from the letter t.
The first line is given the string s and the letter t. Strings and characters are given only in lower case. The length of the string does not exceed 100.
teachermode
eOn the first line, print the distance each character in each string s is separated from the character t, in order.
1 0 1 2 1 0 1 2 2 1 0indexOf() & lastIndexOf()function solution(s, t) {
let answer = '';
const right = (x) => s.indexOf(t, x);
const left = (x) => s.lastIndexOf(t, x);
for (let i = 0; i < s.length; i++) {
let min = Math.min(right(i) - i, i - left(i));
answer += min;
}
return answer;
}indexOf() & lastIndexOf()function solution(s, t) {
let answer = [];
let p = 1000;
for (let x of s) {
if (x === t) {
p = 0;
answer.push(p);
} else {
p++;
answer.push(p);
}
}
p = 1000;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] === t) {
p = 0;
} else {
p++;
answer[i] = Math.min(answer[i], p);
}
}
return answer;
}