Given a string ‘str’, two integers ‘k’ and ‘p’. The task is to count all the sub-strings of ‘str’ having exactly ‘k’ characters that have ASCII value greater than ‘p’.

**Examples:**

Input:str = “abcd”, k=2, p=98

Output:3

Only the characters ‘c’ and ‘d’ have ASCII values greater than 98,

And, the sub-strings containing them are “abcd”, “bcd” and “cd”.

Input:str = “sabrcd”, k=5, p=80

Output:2

**Approach:** We just need to iterate over all indices and take all possible length sub-strings and then just check whether the sub-string has exactly ‘k’ characters that have ASCII value greater than ‘p’.

Typecasting a character to int will give us it’s ASCII value i.e.

`int ascii = (int) char`

.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the approach

# Function that checks if the string

# contain exactly K characters having

# ASCII value greater than p

def isValidSubString(r, K, p):

c = 0

for i in range(0, len(r)):

# if ASCII value is

# greater than ‘p’

if ord(r[i]) > p:

c += 1

# if count of satisfying

# characters is equal to ‘K’

# then return true

if c == K:

return True

# otherwise return false

else:

return False

# function to count sub-strings

def countSubStrings(s, K, p):

# length of the string

l = len(s)

# count of sub-strings

count = 0

# ‘i’ is the starting

# index for the sub-string

for i in range(0, l):

# ‘j’ is the no. of characters

# to include in the sub-string

for j in range(K, l – i + 1):

r = s[i:i + j]

# check if the sub-string

# satisfies the condition

if isValidSubString(r, K, p) == True:

count += 1

print(count)

# Driver code

if __name__ == “__main__”:

s = “abepztydba”

K, p = 4, 110

countSubStrings(s, K, p)

# This code is contributed by Rituraj Jain

**Output:**

16

## Recommended Posts:

- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Count of alphabets having ASCII value less than and greater than k
- Count the number of words having sum of ASCII values less than and greater than k
- Average of ASCII values of characters of a given string
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Count characters in a string whose ASCII values are prime
- Count substrings with same first and last characters
- Count number of substrings with numeric value greater than X
- Program to find the product of ASCII values of characters in a string
- Queries for frequencies of characters in substrings
- Program to find the largest and smallest ASCII valued characters in a string
- Count distinct substrings that contain some characters at most k times
- Recursive solution to count substrings with same first and last characters
- Count number of substrings with exactly k distinct characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.