Number of sub-strings that contain the given character exactly k times
Last Updated :
20 Feb, 2023
Given the string str, a character c, and an integer k > 0. The task is to find the number of sub-strings that contain the character c exactly k times.
Examples:
Input: str = “abada”, c = ‘a’, K = 2
Output: 4
All possible sub-strings are “aba”, “abad”, “bada” and “ada”.
Input: str = “55555”, c = ‘5’, k = 4
Output: 2
Naive approach: A simple solution is to generate all the sub-strings and check whether the count of a given character is exactly k times.
The time complexity of this approach is O(n2).
Space complexity of this approach is O(1).
Efficient approach: An efficient solution is to use the sliding window technique. Find the sub-string that exactly contains the given character k times, starting with character c. Count the number of characters on either side of the sub-string. Multiply the counts to get the number of possible sub-strings. The time complexity of this approach is O(n).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
int main()
{
string s = "abada" ;
char c = 'a' ;
int k = 2;
cout << countSubString(s, c, k) << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countSubString( char [] 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;
}
public static void main(String[] args)
{
String s = "abada" ;
char c = 'a' ;
int k = 2 ;
System.out.println(
countSubString(s.toCharArray(), c, k));
}
}
|
Python3
def countSubString(s, c, k):
leftCount = 0
rightCount = 0
left = 0
right = 0
freq = 0
result = 0
len1 = len (s)
while (s[left] ! = c and left < len1):
left + = 1
leftCount + = 1
right = left + 1
while (freq ! = (k - 1 ) and
(right - 1 ) < len1):
if (s[right] = = c):
freq + = 1
right + = 1
while (left < len1 and
(right - 1 ) < len1):
while (s[left] ! = c and left < len1):
left + = 1
leftCount + = 1
while (right < len1 and
s[right] ! = c):
if (s[right] = = c):
freq + = 1
right + = 1
rightCount + = 1
result = (result + (leftCount + 1 ) *
(rightCount + 1 ))
freq = k - 1
leftCount = 0
rightCount = 0
left + = 1
right + = 1
return result
if __name__ = = '__main__' :
s = "abada"
c = 'a'
k = 2
print (countSubString(s, c, k))
|
C#
using System;
class GFG {
static int countSubString( char [] 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;
}
public static void Main(String[] args)
{
String s = "abada" ;
char c = 'a' ;
int k = 2;
Console.WriteLine(
countSubString(s.ToCharArray(), c, k));
}
}
|
PHP
<?php
function countSubString( $s , $c , $k )
{
$leftCount = 0; $rightCount = 0;
$left = 0; $right = 0;
$freq = 0;
$result = 0; $len = strlen ( $s );
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 ;
}
$s = "abada" ;
$c = 'a' ;
$k = 2;
echo countSubString( $s , $c , $k ), "\n" ;
?>
|
Javascript
<script>
function countSubString(s, c, k)
{
var leftCount = 0, rightCount = 0;
var left = 0, right = 0;
var freq = 0;
var 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;
}
var s = "abada" ;
var c = 'a' ;
var k = 2;
document.write( countSubString(s, c, k) + "<br>" );
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...