Minimize length of string by replacing K pairs of distinct adjacent characters
Last Updated :
07 May, 2021
Given string str of length N, the task is to find the minimum length to which the given string can be reduced by replacing any pair of non-equal adjacent characters with a single character at most K times.
Examples:
Input: str = “aabc”, K =1
Output: 3
Explanation:
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 = 2
Output: 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++
#include <bits/stdc++.h>
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);
}
|
Java
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));
}
}
|
Python3
def MinLen( str , K):
N = len ( str )
i = 0
while (i < N - 1 ):
if ( str [i] ! = str [i + 1 ]):
break
i + = 1
if (i = = N - 1 ):
return N
return max ( 1 , N - K)
if __name__ = = '__main__' :
str = "aabc"
K = 1
print (MinLen( str , K))
|
C#
using System;
class GFG{
static 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 Math.Max(1, N - K);
}
public static void Main(String[] args)
{
String str = "aabc" ;
int K = 1;
Console.Write(MinLen(str, K));
}
}
|
Javascript
<script>
function MinLen( str , K)
{
var N = str.length;
var 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);
}
var str = "aabc" ;
var K = 1;
document.write(MinLen(str, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...