Minimum distance between duplicates in a String | This function is used to find minimum distance between same repeating characters ; Store minimum distance between same repeating characters ; For loop to consider each element of string ; Comparison of string characters and updating the minDis value ; As this value would be least therefore break ; If minDis value is not updated that means no repeating characters ; Minimum distance is minDis - 1 ; Given Input ; Function Call
def shortestDistance ( S , N ) : NEW_LINE INDENT minDis = len ( S ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( S [ i ] == S [ j ] and ( j - i ) < minDis ) : NEW_LINE INDENT minDis = j - i NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( minDis == len ( S ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return minDis - 1 NEW_LINE DEDENT DEDENT S = " geeksforgeeks " NEW_LINE N = 13 NEW_LINE print ( shortestDistance ( S , N ) ) NEW_LINE