Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Notice that it's impossible for any two valid substrings to overlap unless one is inside another.
We can start by finding the starting and ending index for each character.
From these indices, we can form the substrings by expanding each character's range if necessary (if another character exists in the range with smaller/larger starting/ending index).
Sort the valid substrings by length and greedily take those with the smallest length, discarding the ones that overlap those we took.