left
) and rightmost (right
) indices for each character in the string.left
and right
arrays with the corresponding indices for each character.validSubstrings
).validSubstrings
list with its ending index.validSubstrings
list by the ending index in ascending order.lastEnd
).validSubstrings
list and select the non-overlapping substrings by comparing their starting indices with lastEnd
.lastEnd
to the ending index of the selected substring.