Minimum letters to be removed to make all occurrences of a given letter continuous | Function to find the minimum number of deletions required to make the occurrences of the given character K continuous ; Find the first occurrence of the given letter ; Iterate from the first occurrence till the end of the sequence ; Find the index from where the occurrence of the character is not continuous ; Update the answer with the number of elements between non - consecutive occurrences of the given letter ; Update the count for all letters which are not equal to the given letter ; Return the count ; Driver code ; Calling the function ; Calling the function
def noOfDeletions ( string , k ) : NEW_LINE INDENT ans = 0 ; cnt = 0 ; pos = 0 ; NEW_LINE while ( pos < len ( string ) and string [ pos ] != k ) : NEW_LINE INDENT pos += 1 ; NEW_LINE DEDENT i = pos ; NEW_LINE while ( i < len ( string ) ) : NEW_LINE INDENT while ( i < len ( string ) and string [ i ] == k ) : NEW_LINE INDENT i = i + 1 ; NEW_LINE DEDENT ans = ans + cnt ; NEW_LINE cnt = 0 ; NEW_LINE while ( i < len ( string ) and string [ i ] != k ) : NEW_LINE INDENT i = i + 1 ; NEW_LINE cnt = cnt + 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " ababababa " ; NEW_LINE k1 = ' a ' ; NEW_LINE print ( noOfDeletions ( str1 , k1 ) ) ; NEW_LINE str2 = " kprkkoinkopt " ; NEW_LINE k2 = ' k ' ; NEW_LINE print ( noOfDeletions ( str2 , k2 ) ) ; NEW_LINE DEDENT