curriculum/challenges/english/blocks/daily-coding-challenges-javascript/698a1a863194f1f4e63f645e.md
Given a string, return the substring between the two identical characters that have the smallest number of characters between them (smallest gap).
For example, given "ABCDAC", return "DA" because:
"A" and "C" repeat in the string."A" characters is 3, and between the "C" characters is 2."C" characters.smallestGap("ABCDAC") should return "DA".
assert.equal(smallestGap("ABCDAC"), "DA");
smallestGap("racecar") should return "e".
assert.equal(smallestGap("racecar"), "e");
smallestGap("A{5e^SD*F4i!o#q6e&rkf(po8|we9+kr-2!3}=4") should return "#q6e&rkf(p".
assert.equal(smallestGap("A{5e^SD*F4i!o#q6e&rkf(po8|we9+kr-2!3}=4"), "#q6e&rkf(p");
smallestGap("Hello World") should return "".
assert.equal(smallestGap("Hello World"), "");
smallestGap("The quick brown fox jumps over the lazy dog.") should return "fox".
assert.equal(smallestGap("The quick brown fox jumps over the lazy dog."), "fox");
function smallestGap(str) {
return str;
}
function smallestGap(str) {
let minGap = Infinity;
let result = "";
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str[i] === str[j]) {
const gap = j - i - 1;
if (gap < minGap) {
minGap = gap;
result = str.slice(i + 1, j);
}
break;
}
}
}
return result;
}