Sub | C ++ implementation of the approach ; Function that checks if the string contain exactly K characters having ASCII value greater than p ; if ASCII value is greater than ' p ' ; if count of satisfying characters is equal to ' K ' then return true ; otherwise return false ; function to count sub - strings ; length of the string ; count of sub - strings ; ' i ' is the starting index for the sub - string ; ' j ' is the no . of characters to include in the sub - string ; check if the sub - string satisfies the condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValidSubString ( string r , int K , int p ) { int c = 0 ; for ( int i = 0 ; i < r . length ( ) ; i ++ ) { if ( ( int ) r [ i ] > p ) c ++ ; } if ( c == K ) return true ; else return false ; } void countSubStrings ( string s , int K , int p ) { int l = s . length ( ) ; int count = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = K ; ( i + j ) <= l ; j ++ ) { string r = s . substr ( i , j ) ; if ( isValidSubString ( r , K , p ) ) count ++ ; } } cout << count << " STRNEWLINE " ; } int main ( ) { string s = " abepztydba " ; int K = 4 ; int p = 110 ; countSubStrings ( s , K , p ) ; return 0 ; }