Minimize length of string by replacing K pairs of distinct adjacent characters | Java program to implement the above approach ; Function to minimize the length of the String by replacing distinct pairs of adjacent characters ; Stores the length of the given String ; Stores the index of the given String ; Traverse the given String ; If two adjacent characters are distinct ; If all characters are equal ; If all characters are distinct ; Driver Code
class GFG { static int MinLen ( String str , int K ) { int N = str . length ( ) ; int i = 0 ; while ( i < N - 1 ) { if ( str . charAt ( i ) != str . charAt ( i + 1 ) ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return Math . max ( 1 , N - K ) ; } public static void main ( String [ ] args ) { String str = " aabc " ; int K = 1 ; System . out . print ( MinLen ( str , K ) ) ; } }