Minimize length of string by replacing K pairs of distinct adjacent characters | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinLen ( string str , int K ) { int N = str . length ( ) ; int i = 0 ; while ( i < N - 1 ) { if ( str [ i ] != str [ i + 1 ] ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return max ( 1 , N - K ) ; } int main ( ) { string str = " aabc " ; int K = 1 ; cout << MinLen ( str , K ) ; }