Count all substrings having character K
Last Updated :
25 Aug, 2021
Given a string str and a character K, the task is to find the count of all the substrings of str that contain the character K.
Examples:
Input: str = “geeks”, K = ‘g’
Output: 5
“g”, “ge”, “gee”, “geek” and “geeks” are the valid substrings.
Input: str = “geeksforgeeks”, K = ‘k’
Output: 56
Naive approach A simple approach will be to find all the substrings having character K of the given string and return the count;
Efficient approach: For every index i in the string, find the first index j such that i ? j and str[j] = K. Now, the substrings str[i…j], str[i…j + 1], str[i…j + 2], …, str[i…n – 1] will all contain the character K at least once. The approach seems to be O(n2) at first but the index j will not be calculated again for every index i, j will be a valid index for all the values of i less than j.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int nextOccurrence(string str, int n,
int start, char ch)
{
for ( int i = start; i < n; i++) {
if (str[i] == ch)
return i;
}
return -1;
}
int countSubStr(string str, int n, char ch)
{
int cnt = 0;
int j = nextOccurrence(str, n, 0, ch);
for ( int i = 0; i < n; i++) {
while (j != -1 && j < i) {
j = nextOccurrence(str, n, j + 1, ch);
}
if (j == -1)
break ;
cnt += (n - j);
}
return cnt;
}
int main()
{
string str = "geeksforgeeks" ;
int n = str.length();
char ch = 'k' ;
cout << countSubStr(str, n, ch);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int nextOccurrence(String str, int n,
int start, char ch)
{
for ( int i = start; i < n; i++)
{
if (str.charAt(i) == ch)
return i;
}
return - 1 ;
}
static int countSubStr(String str,
int n, char ch)
{
int cnt = 0 ;
int j = nextOccurrence(str, n, 0 , ch);
for ( int i = 0 ; i < n; i++)
{
while (j != - 1 && j < i)
{
j = nextOccurrence(str, n, j + 1 , ch);
}
if (j == - 1 )
break ;
cnt += (n - j);
}
return cnt;
}
static public void main ( String []arg)
{
String str = "geeksforgeeks" ;
int n = str.length();
char ch = 'k' ;
System.out.println(countSubStr(str, n, ch));
}
}
|
Python3
def nextOccurrence(strr, n, start, ch):
for i in range (start, n):
if (strr[i] = = ch):
return i
return - 1
def countSubStr(strr, n, ch):
cnt = 0
j = nextOccurrence(strr, n, 0 , ch)
for i in range (n):
while (j ! = - 1 and j < i):
j = nextOccurrence(strr, n, j + 1 , ch)
if (j = = - 1 ):
break
cnt + = (n - j)
return cnt
strr = "geeksforgeeks"
n = len (strr)
ch = 'k'
print (countSubStr(strr, n, ch))
|
C#
using System;
class GFG
{
static int nextOccurrence(String str, int n,
int start, char ch)
{
for ( int i = start; i < n; i++)
{
if (str[i] == ch)
return i;
}
return -1;
}
static int countSubStr(String str,
int n, char ch)
{
int cnt = 0;
int j = nextOccurrence(str, n, 0, ch);
for ( int i = 0; i < n; i++)
{
while (j != -1 && j < i)
{
j = nextOccurrence(str, n, j + 1, ch);
}
if (j == -1)
break ;
cnt += (n - j);
}
return cnt;
}
static public void Main ()
{
String str = "geeksforgeeks" ;
int n = str.Length;
char ch = 'k' ;
Console.WriteLine(countSubStr(str, n, ch));
}
}
|
Javascript
<script>
function nextOccurrence(str, n, start, ch) {
for ( var i = start; i < n; i++) {
if (str[i] === ch) return i;
}
return -1;
}
function countSubStr(str, n, ch) {
var cnt = 0;
var j = nextOccurrence(str, n, 0, ch);
for ( var i = 0; i < n; i++) {
while (j !== -1 && j < i) {
j = nextOccurrence(str, n, j + 1, ch);
}
if (j === -1) break ;
cnt += n - j;
}
return cnt;
}
var str = "geeksforgeeks" ;
var n = str.length;
var ch = "k" ;
document.write(countSubStr(str, n, ch));
</script>
|
Share your thoughts in the comments
Please Login to comment...