Longest suffix such that occurrence of each character is less than N after deleting atmost K characters | C ++ implementation to find longest suffix of the string such that occurrence of each character is less than K ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSuffix ( string s , int n , int k ) { int i = s . length ( ) - 1 ; int arr [ 26 ] = { 0 } ; string suffix = " " ; while ( i > -1 ) { int index = s [ i ] - ' a ' ; if ( arr [ index ] < n ) { arr [ index ] ++ ; suffix += s [ i ] ; i -- ; continue ; } if ( k == 0 ) break ; k -- ; i -- ; } reverse ( suffix . begin ( ) , suffix . end ( ) ) ; cout << suffix ; } int main ( ) { string str = " iahagafedcba " ; int n = 1 , k = 2 ; maximumSuffix ( str , n , k ) ; return 0 ; }