Find value after N operations to remove N characters of string S with given constraints | C ++ program for the above approach ; Function to find the value after N operations to remove all the N characters of string S ; Iterate till N ; Remove character at ind and decrease n ( size of string ) ; Increase count by ind + 1 ; Driver Code ; Given string str ; Function call
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; void charactersCount ( string str , int n ) { int count = 0 ; while ( n > 0 ) { char cur = str [ 0 ] ; int ind = 0 ; for ( int j = 1 ; j < n ; j ++ ) { if ( str [ j ] < cur ) { cur = str [ j ] ; ind = j ; } } str . erase ( str . begin ( ) + ind ) ; n -- ; count += ind + 1 ; } cout << count << endl ; } int main ( ) { string str = " aabbc " ; int n = 5 ; charactersCount ( str , n ) ; return 0 ; }