Traverse from right and update res as soon as we see a visited character .
res = - 1 ; NEW_LINE for i in range ( len ( string ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( visited [ string . index ( string [ i ] ) ] == False ) : NEW_LINE INDENT visited [ string . index ( string [ i ] ) ] = True ; NEW_LINE DEDENT else : NEW_LINE INDENT res = i ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE