Reduce the string by removing K consecutive identical characters | Java implementation of the approach ; Function to find the reduced string ; Base Case ; Creating a stack of type Pair ; Length of the string S ; iterate through the string ; if stack is empty then simply add the character with count 1 else check if character is same as top of stack ; if character at top of stack is same as current character increase the number of repetitions in the top of stack by 1 ; if character at top of stack is not same as current character push the character along with count 1 into the top of stack ; iterate through the stack Use string ( int , char ) in order to replicate the character multiple times and convert into string then add in front of output string ; Driver code
import java . util . * ; class GFG { public static String reduced_String ( int k , String s ) { if ( k == 1 ) { String ans = " " ; return ans ; } Stack < Pair > st = new Stack < Pair > ( ) ; int l = s . length ( ) ; int ctr = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( st . size ( ) == 0 ) { st . push ( new Pair ( s . charAt ( i ) , 1 ) ) ; continue ; } if ( st . peek ( ) . c == s . charAt ( i ) ) { Pair p = st . peek ( ) ; st . pop ( ) ; p . ctr += 1 ; if ( p . ctr == k ) { continue ; } else { st . push ( p ) ; } } else { st . push ( new Pair ( s . charAt ( i ) , 1 ) ) ; } } String ans = " " ; while ( st . size ( ) > 0 ) { char c = st . peek ( ) . c ; int cnt = st . peek ( ) . ctr ; while ( cnt -- > 0 ) ans = c + ans ; st . pop ( ) ; } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; String st = " geeksforgeeks " ; String ans = reduced_String ( k , st ) ; System . out . println ( ans ) ; } static class Pair { char c ; int ctr ; Pair ( char c , int ctr ) { this . c = c ; this . ctr = ctr ; } } }