Minimum value of K such that each substring of size K has the given character | Function to find the minimum value of K such that char c occurs in all K sized substrings of string S ; Store the string length ; Store difference of lengths of segments of every two consecutive occurrences of c ; Stores the maximum difference ; Store the previous occurence of char c ; Check if the current character is c or not ; Stores the difference of consecutive occurrences of c ; Update previous occurrence of c with current occurence ; Comparing diff with max ; If string doesn 't contain c ; Return max ; Driver Code
def findK ( s , c ) : NEW_LINE INDENT n = len ( s ) NEW_LINE diff = 0 NEW_LINE max = 0 NEW_LINE prev = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( s [ i ] == c ) : NEW_LINE INDENT diff = i - prev NEW_LINE prev = i NEW_LINE if ( diff > max ) : NEW_LINE INDENT max = diff NEW_LINE DEDENT DEDENT DEDENT if ( max == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abdegb " NEW_LINE ch = ' b ' NEW_LINE print ( findK ( S , ch ) ) NEW_LINE DEDENT