Minimum K such that every substring of length at least K contains a character c | Set | Function to find minimum value of K such that there exist atleast one K amazing character ; Stores the answer ; Update the S ; Iterate over the characters in range [ a , z ] ; Stores the last index where c appears ; Stores the maximum possible length ; Update S ; Iterate over characters of string S ; If S [ i ] is equal to c ; Stores the distance between positions of two same c ; Update max_len ; Update the value of prev ; Update the value of ans ; Return the answer ; Driver Code ; Given Input ; Function Call
def MinimumLengthSubstring ( S , N ) : NEW_LINE INDENT ans = N NEW_LINE S = "0" + S + "0" NEW_LINE S = [ i for i in S ] NEW_LINE for c in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT prev = 0 NEW_LINE max_len = 0 NEW_LINE S [ 0 ] = chr ( c ) NEW_LINE S [ N + 1 ] = chr ( c ) NEW_LINE for i in range ( 1 , N + 2 ) : NEW_LINE INDENT if ( S [ i ] == chr ( c ) ) : NEW_LINE INDENT len = i - prev NEW_LINE max_len = max ( max_len , len ) NEW_LINE prev = i NEW_LINE DEDENT DEDENT ans = min ( ans , max_len ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcde " NEW_LINE N = len ( S ) NEW_LINE print ( MinimumLengthSubstring ( S , N ) ) NEW_LINE DEDENT