Minimum removals required such that given string consists only of a pair of alternating characters | Function to find the maximum length of alternating occurrences of a pair of characters in a string s ; Stores the next character for alternating sequence ; Stores the length of alternating occurrences of a pair of characters ; Traverse the given string ; If current character is same as the required character ; Increase length by 1 ; Reset required character ; Return the length ; Function to find minimum characters required to be deleted from S to obtain an alternating sequence ; Stores maximum length of alternating sequence of two characters ; Stores length of the string ; Generate every pair of English alphabets ; Function call to find length of alternating sequence for current pair of characters ; Update len to store the maximum of len and newLen in len ; Return n - len as the final result ; Driver Code ; Given Input ; Function call to find minimum characters required to be removed from S to make it an alternating sequence of a pair of characters
def findLength ( s , i , j ) : NEW_LINE INDENT required = i NEW_LINE length = 0 NEW_LINE for curr in s : NEW_LINE INDENT if ( curr == required ) : NEW_LINE INDENT length += 1 NEW_LINE if ( required == i ) : NEW_LINE INDENT required = j NEW_LINE DEDENT else : NEW_LINE INDENT required = i NEW_LINE DEDENT DEDENT DEDENT return length NEW_LINE DEDENT def minimumDeletions ( S ) : NEW_LINE INDENT len1 = 0 NEW_LINE n = len ( S ) NEW_LINE for i in range ( 0 , 26 , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , 26 , 1 ) : NEW_LINE INDENT newLen = findLength ( S , chr ( i + 97 ) , chr ( j + 97 ) ) NEW_LINE len1 = max ( len1 , newLen ) NEW_LINE DEDENT DEDENT return n - len1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " adebbeeaebd " NEW_LINE print ( minimumDeletions ( S ) ) NEW_LINE DEDENT