Given string **str** of length **N**, the task is to find the minimum length to which the given string can be reduced to by replacing any pair of non-equal adjacent characters with a single character at most **K** times.

**Examples:**

Input: str = “aabc”, K =1Output:3Explanation:

Replace “bc” with a single character “d” and decrement K by 1.

Therefore, str modifies to “aad”.

Since K = 0, the minimized length of the string is 3.

Input:str= “aabc”, K = 2Output:2

**Naive Approach:** The simplest approach is to traverse the string **K** times and during each traversal, check if any pair of adjacent characters of the given string are distinct or not. If found to be true, then replace both the characters with a single character that is not equal to its adjacent characters. Finally, print the minimum length of the string.

**Time Complexity:** O(N * K)**Auxiliary Space:** O(1)

**Efficient Approach:** To optimize the above approach, the idea is to traverse the given string and check if any pair of adjacent characters of the given string are distinct or not. If found to be true, then print the value of **max(1, (N – K))**. Otherwise, print **N**. Follow the steps below to solve the problem:

- Traverse the given string
**str**and check if any pair of adjacent characters are distinct or not. - If found to be true, then print
**max(1, (N – K))**as the required answer. - Otherwise, print
**N**as the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to minimize the length` `// of the string by replacing distinct` `// pairs of adjacent characters` `int` `MinLen(string str, ` `int` `K)` `{` ` ` `// Stores the length` ` ` `// of the given string` ` ` `int` `N = str.length();` ` ` `// Stores the index` ` ` `// of the given string` ` ` `int` `i = 0;` ` ` `// Traverse the given string` ` ` `while` `(i < N - 1) {` ` ` `// If two adjacent` ` ` `// characters are distinct` ` ` `if` `(str[i] != str[i + 1]) {` ` ` `break` `;` ` ` `}` ` ` `i++;` ` ` `}` ` ` `// If all characters` ` ` `// are equal` ` ` `if` `(i == N - 1) {` ` ` `return` `N;` ` ` `}` ` ` `// If all characters` ` ` `// are distinct` ` ` `return` `max(1, N - K);` `}` `// Driver Code` `int` `main()` `{` ` ` `string str = ` `"aabc"` `;` ` ` `int` `K = 1;` ` ` `cout << MinLen(str, K);` `}` |

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to minimize the length` `# of the by replacing distinct` `# pairs of adjacent characters` `def` `MinLen(` `str` `, K):` ` ` ` ` `# Stores the length` ` ` `# of the given string` ` ` `N ` `=` `len` `(` `str` `)` ` ` `# Stores the index` ` ` `# of the given string` ` ` `i ` `=` `0` ` ` `# Traverse the given string` ` ` `while` `(i < N ` `-` `1` `):` ` ` `# If two adjacent` ` ` `# haracters are distinct` ` ` `if` `(` `str` `[i] !` `=` `str` `[i ` `+` `1` `]):` ` ` `break` ` ` ` ` `i ` `+` `=` `1` ` ` `# If all characters` ` ` `# are equal` ` ` `if` `(i ` `=` `=` `N ` `-` `1` `):` ` ` `return` `N` ` ` `# If all characters` ` ` `# are distinct` ` ` `return` `max` `(` `1` `, N ` `-` `K)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `str` `=` `"aabc"` ` ` `K ` `=` `1` ` ` ` ` `print` `(MinLen(` `str` `, K))` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Function to minimize the ` `// length of the String by ` `// replacing distinct pairs ` `// of adjacent characters` `static` `int` `MinLen(String str, ` ` ` `int` `K)` `{` ` ` `// Stores the length` ` ` `// of the given String` ` ` `int` `N = str.Length;` ` ` `// Stores the index` ` ` `// of the given String` ` ` `int` `i = 0;` ` ` `// Traverse the given String` ` ` `while` `(i < N - 1) ` ` ` `{` ` ` `// If two adjacent` ` ` `// characters are distinct` ` ` `if` `(str[i] != str[i + 1]) ` ` ` `{` ` ` `break` `;` ` ` `}` ` ` `i++;` ` ` `}` ` ` `// If all characters` ` ` `// are equal` ` ` `if` `(i == N - 1) ` ` ` `{` ` ` `return` `N;` ` ` `}` ` ` `// If all characters` ` ` `// are distinct` ` ` `return` `Math.Max(1, N - K);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `String str = ` `"aabc"` `;` ` ` `int` `K = 1;` ` ` `Console.Write(MinLen(str, K));` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Minimize Array length by repeatedly replacing co-prime pairs with 1
- String with k distinct characters and no same characters adjacent
- Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum
- Minimize length of prefix of string S containing all characters of another string T
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Minimize the cost to make all the adjacent elements distinct in an Array
- Find distinct characters in distinct substrings of a string
- Smallest length string with repeated replacement of two distinct adjacent
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Minimum string such that every adjacent character of given string is still adjacent
- Count distinct strings possible by replacing each character by its Morse code

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.