Wednesday, September 7, 2022
HomeWordPress DevelopmentReduce String size by deleting Substring of identical character when Okay flips...

Reduce String size by deleting Substring of identical character when Okay flips are allowed


#embody <bits/stdc++.h>

utilizing namespace std;

bool examine(string s, int Okay, int restrict)

{

  

    

    

    int rely[2] = { 0 };

  

    

    

    for (int i = 0; i < restrict; i++) {

        rely[s[i] - '0']++;

    }

  

    

    

    

    

    if (Okay >= min(rely[0], rely[1]))

        return true;

  

    

    for (int i = 0; i + restrict < s.measurement(); i++) {

  

        

        

        rely[s[i] - '0']--;

  

        

        

        

        rely[s[i + limit] - '0']++;

  

        

        

        

        if (Okay >= min(rely[0], rely[1]))

            return true;

    }

  

    

    

    return false;

}

  

void clear up(string s, int n, int Okay)

{

  

    

    int Low = 1, Excessive = n;

  

    

    int ans = 0;

    whereas (Low <= Excessive) {

  

        

        int mid = Low + (Excessive - Low) / 2;

  

        

        

        if (examine(s, Okay, mid)) {

  

            

            

            

            ans = max(ans, mid);

  

            

            

            

            

            

            Low = mid + 1;

        }

        else {

  

            

            Excessive = mid - 1;

        }

    }

  

    

    cout << "Minimal size after deletion is: ";

    int res = n - ans;

    cout << res;

}

  

int most important()

{

  

    

    string s = "0110";

    int Okay = 2;

    int n = s.measurement();

  

    

    clear up(s, n, Okay);

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments