Saturday, October 14, 2023
HomeSoftware 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 ok)

{

    int n = s.dimension();

  

    

    

    

    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, ok - 1, diff);

  

    

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

  

    

    

    

    return val1 - val2 - val3;

}

  

int major()

{

    string S = "ecadgg";

    int Okay = 4;

  

    

    cout << numberOfSubstrings(S, Okay);

  

    return 0;

}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments