Sub-strings having exactly k characters that have ASCII value greater than p
Last Updated :
08 Feb, 2024
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 values 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++
#include <bits/stdc++.h>
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 << "\n" ;
}
int main()
{
string s = "abepztydba" ;
int K = 4;
int p = 110;
countSubStrings(s, K, p);
return 0;
}
|
Java
class GFG {
boolean isValidSubString(String r, int K, int p)
{
int c = 0 ;
for ( int i = 0 ; i < r.length(); i++) {
if (( int )r.charAt(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.substring(i, (i + j));
if (isValidSubString(r, K, p)) {
count++;
}
}
}
System.out.println(count);
}
public static void main(String args[])
{
GFG g = new GFG();
String s = "abepztydba" ;
int K = 4 ;
int p = 110 ;
g.countSubStrings(s, K, p);
}
}
|
Python3
def isValidSubString(r, K, p):
c = 0
for i in range ( 0 , len (r)):
if ord (r[i]) > p:
c + = 1
if c = = K:
return True
else :
return False
def countSubStrings(s, K, p):
l = len (s)
count = 0
for i in range ( 0 , l):
for j in range (K, l - i + 1 ):
r = s[i:i + j]
if isValidSubString(r, K, p) = = True :
count + = 1
print (count)
if __name__ = = "__main__" :
s = "abepztydba"
K, p = 4 , 110
countSubStrings(s, K, p)
|
C#
using System;
class GFG {
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.Substring(i, j);
if (isValidSubString(r, K, p)) {
count++;
}
}
}
Console.WriteLine(count);
}
public static void Main(String[] args)
{
GFG g = new GFG();
String s = "abepztydba" ;
int K = 4;
int p = 110;
g.countSubStrings(s, K, p);
}
}
|
Javascript
<script>
function isValidSubString(r, K, p)
{
var c = 0;
for ( var i = 0; i < r.length; i++) {
if (r.charCodeAt(i) > p)
c++;
}
if (c == K)
return true ;
else
return false ;
}
function countSubStrings(s, K, p)
{
var l = s.length;
var count = 0;
for ( var i = 0; i < l; i++) {
for ( var j = K; (i + j) <= l; j++) {
var r = s.substring(i, i+j);
if (isValidSubString(r, K, p))
count++;
}
}
document.write( count + "<br>" );
}
var s = "abepztydba" ;
var K = 4;
var p = 110;
countSubStrings(s, K, p);
</script>
|
Complexity Analysis:
- Time Complexity: O(N3), as we are using nested loops to traverse N*N times and also using inbuilt substring function which will cost us O(N) time.
- Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...