Friday, October 14, 2022
HomeWordPress DevelopmentRely odd size Substrings with median similar as Kth character of String

Rely odd size Substrings with median similar as Kth character of String


  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int sum(int begin, int finish, vector<int>& v)

{

    

    

    

    unordered_map<int, int> prevSum;

  

    int res = 0, currSum = 0;

  

    for (int i = begin; i < finish; i++) {

  

        

        currSum += v[i];

  

        

        

        

        if (currSum == 0)

            res++;

  

        if (prevSum.discover(currSum) != prevSum.finish())

            res += (prevSum[currSum]);

  

        

        

        prevSum[currSum]++;

    }

  

    return res;

}

  

int numberOfSubstrings(string& s, int okay)

{

    int n = s.measurement();

  

    

    

    

    vector<int> smaller(n, 0), higher(n, 0);

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

        smaller[i] = s[i] < s[k - 1];

  

        higher[i] = s[i] > s[k - 1];

    }

  

    

    

    

    vector<int> diff(n, 0);

    for (int i = 0; i < n; i++)

        diff[i] = smaller[i] - higher[i];

  

    

    int val1 = sum(0, n, diff);

  

    

    int val2 = sum(0, okay - 1, diff);

  

    

    int val3 = sum(okay, n, diff);

  

    

    

    

    return val1 - val2 - val3;

}

  

int major()

{

    string S = "ecadgg";

    int Okay = 4;

  

    

    cout << numberOfSubstrings(S, 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