Longest suffix such that occurrence of each character is less than N after deleting atmost K characters | Function to find the maximum length suffix in the string ; Length of the string ; Map to store the number of occurrence of character ; Loop to iterate string from the last character ; Condition to check if the occurrence of each character is less than given number ; Condition when character cannot be deleted ; Longest suffix ; Driver Code ; Function call
def maximumSuffix ( s , n , k ) : NEW_LINE INDENT i = len ( s ) - 1 NEW_LINE arr = [ 0 for i in range ( 26 ) ] NEW_LINE suffix = " " NEW_LINE while ( i > - 1 ) : NEW_LINE INDENT index = ord ( s [ i ] ) - ord ( ' a ' ) ; NEW_LINE if ( arr [ index ] < n ) : NEW_LINE INDENT arr [ index ] += 1 NEW_LINE suffix += s [ i ] NEW_LINE i -= 1 NEW_LINE continue NEW_LINE DEDENT if ( k == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT k -= 1 NEW_LINE i -= 1 NEW_LINE DEDENT suffix = suffix [ : : - 1 ] NEW_LINE print ( suffix ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " iahagafedcba " NEW_LINE n = 1 NEW_LINE k = 2 NEW_LINE maximumSuffix ( str , n , k ) NEW_LINE DEDENT