Task is to determine the count of substrings, where the count of digit C is exactly P
Input Format: The first line contains integer t denoting the number of test cases, For the next 2*t (2 lines for each test case), the first line contains two integers P and C, and the second line contains a string. Let us denote the string by symbol s. Constraints:
1<=t<=10
1<=P,|s|<=1000000
0<=C<=9
Time Limit: 1 second Output Format: For each test case, the first and only line of output contains the answer as described in task.
Sample Input 1:
2
1 2
212
4 5
55555
Sample Output 1:
4
2
Explanation: For First test case: {2}, {2,1}, {1,2}, {2} are the substrings that contains digit 2 exactly 1 time.
For Second test case: {5,5,5,5}, {5,5,5,5} are the substrings that contains digit 5 exactly 4 times .
I am getting runtime error in this
using namespace std;
int countSubString(string s, char c, int k)
{
int leftCount = 0, rightCount = 0;
int left = 0, right = 0;
int freq = 0;
int result = 0, len = s.length();
while (s[left] != c && left < len) {
left++;
leftCount++;
}
right = left + 1;
while (freq != (k - 1) && (right - 1) < len) {
if (s[right] == c)
freq++;
right++;
}
while (left < len && (right - 1) < len) {
while (s[left] != c && left < len) {
left++;
leftCount++;
}
while (right < len && s[right] != c) {
if (s[right] == c)
freq++;
right++;
rightCount++;
}
result = result + (leftCount + 1) * (rightCount + 1);
freq = k - 1;
leftCount = 0;
rightCount = 0;
left++;
right++;
}
return result;
}
// Driver code
int main()
{
int t;
cin>>t;
while(t--){
string s;
cin>> s[1000000];
char c;
cin>>c;
int k;
cin>>k;
cout << countSubString(s, c, k);
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire