curriculum/challenges/english/blocks/daily-coding-challenges-python/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.smallest_gap("ABCDAC") should return "DA".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(smallest_gap("ABCDAC"), "DA")`)
}})
smallest_gap("racecar") should return "e".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(smallest_gap("racecar"), "e")`)
}})
smallest_gap("A{5e^SD*F4i!o#q6e&rkf(po8|we9+kr-2!3}=4") should return "#q6e&rkf(p".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(smallest_gap("A{5e^SD*F4i!o#q6e&rkf(po8|we9+kr-2!3}=4"), "#q6e&rkf(p")`)
}})
smallest_gap("Hello World") should return "".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(smallest_gap("Hello World"), "")`)
}})
smallest_gap("The quick brown fox jumps over the lazy dog.") should return "fox".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(smallest_gap("The quick brown fox jumps over the lazy dog."), "fox")`)
}})
def smallest_gap(s):
return s
def smallest_gap(s):
min_gap = float("inf")
result = ""
for i in range(len(s)):
for j in range(i + 1, len(s)):
if s[i] == s[j]:
gap = j - i - 1
if gap < min_gap:
min_gap = gap
result = s[i + 1:j]
break
return result