Find value after N operations to remove N characters of string S with given constraints | Java 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
import java . util . * ; class GFG { static void charactersCount ( String str , int n ) { int count = 0 ; while ( n > 0 ) { char cur = str . charAt ( 0 ) ; int ind = 0 ; for ( int j = 1 ; j < n ; j ++ ) { if ( str . charAt ( j ) < cur ) { cur = str . charAt ( j ) ; ind = j ; } } str = str . substring ( 0 , ind ) + str . substring ( ind + 1 ) ; n -- ; count += ind + 1 ; } System . out . print ( count + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " aabbc " ; int n = 5 ; charactersCount ( str , n ) ; } }