Depend odd size Substrings with median identical as Kth character of String

[ad_1]

  

#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.dimension();

  

    

    

    

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

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

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

  

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

    }

  

    

    

    

    vector<int> diff(n, 0);

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

        diff[i] = smaller[i] - better[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 most important()

{

    string S = "ecadgg";

    int Ok = 4;

  

    

    cout << numberOfSubstrings(S, Ok);

  

    return 0;

}

[ad_2]

Leave a Reply