To store max Index encountred so far
index = - 1 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( S [ j ] == ch ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( count == K ) : NEW_LINE INDENT index = j NEW_LINE DEDENT DEDENT answer . append ( [ ch , index ] ) NEW_LINE flag = 0 NEW_LINE