Minimize String length by deleting Substring of same character when K flips are allowed
Last Updated :
03 Nov, 2022
Given a binary string consisting of ‘0’ and ‘1’ only and an integer K, the task is to minimize the string as far as possible by deleting a substring of the same character, when you can flip at most K characters.
Examples:
Input: S = “0110”, K = 2
Output: 0
Explanation: We can make two ‘0’ s into ‘1’ or vice versa.
So we can delete all the four characters and
the remaining size of the string is 0
Input: S = “111”, K = 1
Output: 0
Approach: To solve the problem follow the below idea:
As we have to minimize the string by deleting characters so we have to delete the maximum length of substring consisting of equal letters. So, we can understand the minimum length will range from 1 to the size of the string(let’s say n).
Now if we apply binary search on this range of length and check for a shorter length until it is out of range, we can get the answer.
Follow the steps to solve the problem:
- At first, for the binary search, we will define our range from Low = 1 to High = n(size of string).
- Then after finding the mid we will keep passing the mid to check function to get one of the possible answers and keep incrementing the low to mid+1 as we want the maximum length
- In the check function, first, we will create an array of sizes to 2 to count ‘0’ and ‘1’.
- Then, we will count ‘0’ and ‘1’ and store in that count array, and if the minimum of the count of both elements is less than K that means we can change those characters to the same character to increase the goodness of the string.
- After that will slide the window by mid(limit) size and decrease the count of the first element of the window as the window is not there anymore and increase the count of the new element as it is the new and last element of the window
- Now, we will check again the minimum of the count of ‘0’ s and ‘1’s, if it is less than or equals to K, then return true otherwise false
- We will store every possible answer to the ans variable and when low ? high the loop will terminate and we have our maximum possible length for deletion stored in ans variable.
- Now we will subtract the maximum length from the size of the array to get the size of our minimized string
Below is the implementation for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string s, int K, int limit)
{
int count[2] = { 0 };
for ( int i = 0; i < limit; i++) {
count[s[i] - '0' ]++;
}
if (K >= min(count[0], count[1]))
return true ;
for ( int i = 0; i + limit < s.size(); i++) {
count[s[i] - '0' ]--;
count[s[i + limit] - '0' ]++;
if (K >= min(count[0], count[1]))
return true ;
}
return false ;
}
void solve(string s, int n, int K)
{
int Low = 1, High = n;
int ans = 0;
while (Low <= High) {
int mid = Low + (High - Low) / 2;
if (check(s, K, mid)) {
ans = max(ans, mid);
Low = mid + 1;
}
else {
High = mid - 1;
}
}
cout << "Minimum length after deletion is: " ;
int res = n - ans;
cout << res;
}
int main()
{
string s = "0110" ;
int K = 2;
int n = s.size();
solve(s, n, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean check(String s, int K, int limit)
{
int [] count = new int [ 2 ];
for ( int i = 0 ; i < limit; i++) {
count[s.charAt(i) - '0' ]++;
}
if (K >= Math.min(count[ 0 ], count[ 1 ]))
return true ;
for ( int i = 0 ; i + limit < s.length(); i++) {
count[s.charAt(i) - '0' ]--;
count[s.charAt(i + limit) - '0' ]++;
if (K >= Math.min(count[ 0 ], count[ 1 ]))
return true ;
}
return false ;
}
static void solve(String s, int n, int K)
{
int Low = 1 , High = n;
int ans = 0 ;
while (Low <= High) {
int mid = Low + (High - Low) / 2 ;
if (check(s, K, mid)) {
ans = Math.max(ans, mid);
Low = mid + 1 ;
}
else {
High = mid - 1 ;
}
}
System.out.print(
"Minimum length after deletion is: " );
int res = n - ans;
System.out.println(res);
}
public static void main(String[] args)
{
String s = "0110" ;
int K = 2 ;
int n = s.length();
solve(s, n, K);
}
}
|
Python3
def check(s, K, limit) :
count = [ 0 , 0 ]
for i in range ( 0 ,limit):
count[ int (s[i])] + = 1
if (K > = min (count[ 0 ],count[ 1 ])):
return True
for i in range ( 0 ,( len (s) - limit)):
count[s[i] - '0' ] - = 1
count[s[i + limit] - '0' ] + = 1
if (K > = min (count[ 0 ], count[ 1 ])):
return True
return False
def solve(s, n, K):
Low = 1
High = n
ans = 0
while (Low < = High):
mid = int (Low + (High - Low) / 2 )
if (check(s, K, mid) is True ) :
ans = max (ans, mid)
Low = mid + 1
else :
High = mid - 1
res = n - ans
print ( "Minimum length after deletion is:" ,res)
s = "0110"
K = 2
n = len (s)
solve(s, n, K)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool check( string s, int K, int limit)
{
int [] count = new int [2];
for ( int i = 0; i < limit; i++) {
count[s[i] - '0' ]++;
}
if (K >= Math.Min(count[0], count[1]))
return true ;
for ( int i = 0; i + limit < s.Length; i++) {
count[s[i] - '0' ]--;
count[s[i + limit] - '0' ]++;
if (K >= Math.Min(count[0], count[1]))
return true ;
}
return false ;
}
static void solve( string s, int n, int K)
{
int Low = 1, High = n;
int ans = 0;
while (Low <= High) {
int mid = Low + (High - Low) / 2;
if (check(s, K, mid)) {
ans = Math.Max(ans, mid);
Low = mid + 1;
}
else {
High = mid - 1;
}
}
Console.Write( "Minimum length after deletion is: " );
int res = n - ans;
Console.WriteLine(res);
}
public static void Main(String[] args)
{
string s = "0110" ;
int K = 2;
int n = s.Length;
solve(s, n, K);
}
}
|
Javascript
function check(s, K, limit) {
let count = [0, 0];
for (let i = 0; i < limit; i++) {
count[s[i] - '0' ] = count[s[i] - '0' ] + 1;
}
if (K >= Math.min(count[0], count[1]))
return true ;
for (let i = 0; i + limit < s.length; i++) {
count[s[i] - '0' ]--;
count[s[i + limit] - '0' ]++;
if (K >= Math.min(count[0], count[1]))
return true ;
}
return false ;
}
function solve(s, n, K) {
let Low = 1, High = n;
let ans = 0;
while (Low <= High) {
let mid = Math.floor(Low + (High - Low) / 2);
console.log( "mid : " , mid);
if (check(s, K, mid)) {
ans = Math.max(ans, mid);
Low = mid + 1;
}
else {
High = mid - 1;
}
}
let res = n - ans;
console.log( "Minimum length after deletion is:" , res);
}
let s = "0110" ;
let K = 2;
let n = s.length;
solve(s, n, K);
|
Output
Minimum length after deletion is: 0
Time Complexity: O(N * logN), as the size of the string, is N and we are applying binary search that has logN complexity. So complexity O(N*logN).
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...