Minimum substring removals required to make all remaining characters of a string same | ''Python3 program to implement the above approach ; ''Function to count minimum operations required to make all characters equal by repeatedly removing substring ; '' Remove consecutive duplicate  characters from str ; '' Stores length of the string ; '' Stores frequency of each distinct  characters of the string str ; '' Iterate over all the characters  of the string str ; '' Update frequency of str[i] ; '' Decrementing the frequency  of the string str[0] ; '' Decrementing the frequency  of the ord(string ord(str[N - 1] ; '' Stores the required count ; '' Iterate over all characters  of the string str ; '' Update ans ; ''Driver Code ; '' Given string
import re , sys NEW_LINE def minOperationNeeded ( s ) : NEW_LINE INDENT d = { } NEW_LINE str = re . sub ( r " ( . ) \1 ▁ + ▁ " , ' ' , s ) NEW_LINE N = len ( str ) NEW_LINE res = [ 0 for i in range ( 256 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT res [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT res [ ord ( str [ 0 ] ) ] -= 1 NEW_LINE res [ ord ( str [ N - 1 ] ) ] -= 1 NEW_LINE ans = sys . maxsize NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans = min ( ans , res [ ord ( str [ i ] ) ] ) NEW_LINE DEDENT print ( ( ans + 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ABCDABCDABCDA " NEW_LINE minOperationNeeded ( str ) NEW_LINE DEDENT