Skip to content
Related Articles

Related Articles

Minimize String length by deleting Substring of same character when K flips are allowed

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Sep, 2022
View Discussion
Improve Article
Save Article

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++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
bool check(string s, int K, int limit)
{
  
    // Count array for counting
    // '0's and '1's
    int count[2] = { 0 };
  
    // Counting '0' and '1's in
    // window size
    for (int i = 0; i < limit; i++) {
        count[s[i] - '0']++;
    }
  
    // If the minimum of '0's count and
    // '1's count less than or equals to
    // k it is one of the possible
    // answer, returnn true
    if (K >= min(count[0], count[1]))
        return true;
  
    // Sliding the window
    for (int i = 0; i + limit < s.size(); i++) {
  
        // Decrementing the count of first
        // element as window is slided
        count[s[i] - '0']--;
  
        // Incrementing the count of new
        // element as this is the last
        // element of new window
        count[s[i + limit] - '0']++;
  
        // Checking the minimum count of
        // '0's and '1's because it is
        // the new window
        if (K >= min(count[0], count[1]))
            return true;
    }
  
    // If the above conditions are not
    // fullfilled return false
    return false;
}
  
void solve(string s, int n, int K)
{
  
    // Defining range 1 to n
    int Low = 1, High = n;
  
    // Initializing ans to 0
    int ans = 0;
    while (Low <= High) {
  
        // Calculating mid
        int mid = Low + (High - Low) / 2;
  
        // Passing the mid to
        // check function
        if (check(s, K, mid)) {
  
            // We got one of the possible
            // answers so storing it in
            // ans variable
            ans = max(ans, mid);
  
            // Also incrementing the low
            // to check whether there is
            // any maximum length string
            // with maximum goodness
            // present or not
            Low = mid + 1;
        }
        else {
  
            // Decrementing the high
            High = mid - 1;
        }
    }
  
    // Printing the ans
    cout << "Minimum length after deletion is: ";
    int res = n - ans;
    cout << res;
}
  
// Driver Code
int main()
{
  
    // Given input
    string s = "0110";
    int K = 2;
    int n = s.size();
  
    // Function call
    solve(s, n, K);
    return 0;
}

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)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!