Count of sub-strings that do not consist of the given character
Given a string str and a character c. The task is to find the number of sub-strings that do not consist of the character c.
Examples:
Input: str = “baa”, c = ‘b’
Output: 3
The sub-strings are “a”, “a” and “aa”
Input: str = “ababaa”, C = ‘b’
Output: 5
Approach: Initially take a counter that counts the number of characters continuously with no character c. Iterate in the string and increase the counter till str[i] != c. Once str[i] == c, the number of sub-strings from the contiguous length cnt will be (cnt * (cnt + 1)) / 2. After the complete traversal of the string also add (cnt *(cnt + 1)) / 2 to the result for the group of characters appearing after the last occurrence of c.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubstrings(string s, char c)
{
int n = s.length();
int cnt = 0;
int sum = 0;
for ( int i = 0; i < n; i++) {
if (s[i] != c)
cnt++;
else {
sum += (cnt * (cnt + 1)) / 2;
cnt = 0;
}
}
sum += (cnt * (cnt + 1)) / 2;
return sum;
}
int main()
{
string s = "baa" ;
char c = 'b' ;
cout << countSubstrings(s, c);
return 0;
}
|
Java
class GFG
{
static int countSubstrings(String s, char c)
{
int n = s.length();
int cnt = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) != c)
cnt++;
else
{
sum += (cnt * (cnt + 1 )) / 2 ;
cnt = 0 ;
}
}
sum += (cnt * (cnt + 1 )) / 2 ;
return sum;
}
public static void main(String[] args)
{
String s = "baa" ;
char c = 'b' ;
System.out.println(countSubstrings(s, c));
}
}
|
Python3
def countSubstrings(s, c):
n = len (s)
cnt = 0
Sum = 0
for i in range (n):
if (s[i] ! = c):
cnt + = 1
else :
Sum + = (cnt * (cnt + 1 )) / / 2
cnt = 0
Sum + = (cnt * (cnt + 1 )) / / 2
return Sum
s = "baa"
c = 'b'
print (countSubstrings(s, c))
|
C#
using System;
class GFG
{
static int countSubstrings( string s, char c)
{
int n = s.Length;
int cnt = 0;
int sum = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] != c)
cnt++;
else
{
sum += (cnt * (cnt + 1)) / 2;
cnt = 0;
}
}
sum += (cnt * (cnt + 1)) / 2;
return sum;
}
public static void Main()
{
string s = "baa" ;
char c = 'b' ;
Console.Write(countSubstrings(s, c));
}
}
|
PHP
<?php
function countSubstrings( $s , $c )
{
$n = strlen ( $s );
$cnt = 0;
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] != $c )
$cnt ++;
else
{
$sum += floor (( $cnt * ( $cnt + 1)) / 2);
$cnt = 0;
}
}
$sum += floor (( $cnt * ( $cnt + 1)) / 2);
return $sum ;
}
$s = "baa" ;
$c = 'b' ;
echo countSubstrings( $s , $c );
?>
|
Javascript
<script>
function countSubstrings( s, c) {
var n = s.length;
var cnt = 0;
var sum = 0;
for (i = 0; i < n; i++) {
if (s.charAt(i) != c)
cnt++;
else {
sum += (cnt * (cnt + 1)) / 2;
cnt = 0;
}
}
sum += (cnt * (cnt + 1)) / 2;
return sum;
}
var s = "baa" ;
var c = 'b' ;
document.write(countSubstrings(s, c));
</script>
|
Time Complexity: O(n) where n is the length of the string
Auxiliary Space: O(1)
New Approach:- Here an alternative approach to count the number of substrings that do not contain a given character in a string
- Initialize a counter variable to 0.
- Initialize two pointers, start and end, to 0.
- Find the length of the string and store it in the variable n.
- Traverse the string using the end pointer until it reaches the end of the string.
- If the current character is equal to the given character, calculate the number of substrings that do not contain the given character using the formula (end – start) * (end – start + 1) / 2, and add it to the counter variable.
- Reset the start pointer to the next character after the current character.
- Increment the end pointer.
- Calculate the number of substrings that do not contain the given character for the remaining characters in the string, and add it to the counter variable.
- Return the final count.
- In the main function, initialize the input string and character, call the countSubstrings function, store the returned value in a variable, and print it.
Here’s the implementation of the above approach in Java:
C++
#include <iostream>
#include <string>
using namespace std;
int countSubstrings(string s, char c)
{
int count
= 0;
int start
= 0;
int end = 0;
int n = s.length();
while (end < n) {
if (s[end] == c) {
count += (end - start) * (end - start + 1) / 2;
start = end + 1;
}
end++;
}
count += (end - start) * (end - start + 1) / 2;
return count;
}
int main()
{
string s = "baa" ;
char c = 'b' ;
int count = countSubstrings(
s, c);
cout << count << endl;
return 0;
}
|
Java
public class Solution {
public static void main(String[] args) {
String s = "baa" ;
char c = 'b' ;
int count = countSubstrings(s, c);
System.out.println(count);
}
public static int countSubstrings(String s, char c) {
int count = 0 ;
int start = 0 ;
int end = 0 ;
int n = s.length();
while (end < n) {
if (s.charAt(end) == c) {
count += (end - start) * (end - start + 1 ) / 2 ;
start = end + 1 ;
}
end++;
}
count += (end - start) * (end - start + 1 ) / 2 ;
return count;
}
}
|
Python3
def countSubstrings(s, c):
count = 0
start = 0
end = 0
n = len (s)
while end < n:
if s[end] = = c:
count + = (end - start) * (end - start + 1 ) / / 2
start = end + 1
end + = 1
count + = (end - start) * (end - start + 1 ) / / 2
return count
s = "baa"
c = 'b'
count = countSubstrings(s, c)
print (count)
|
C#
using System;
class Program {
static int CountSubstrings( string s, char c)
{
int count
= 0;
int start
= 0;
int end
= 0;
int n = s.Length;
while (end < n) {
if (s[end] == c) {
count += (end - start) * (end - start + 1)
/ 2;
start = end + 1;
}
end++;
}
count += (end - start) * (end - start + 1) / 2;
return count;
}
static void Main( string [] args)
{
string s = "baa" ;
char c = 'b' ;
int count = CountSubstrings(
s, c);
Console.WriteLine(count);
}
}
|
Javascript
function countSubstrings(s, c) {
let count = 0;
let start = 0;
let end = 0;
const n = s.length;
while (end < n) {
if (s[end] === c) {
count += ((end - start) * (end - start + 1)) / 2;
start = end + 1;
}
end++;
}
count += ((end - start) * (end - start + 1)) / 2;
return count;
}
const s = "baa" ;
const c = "b" ;
const count = countSubstrings(s, c);
console.log(count);
|
In this code, added the main method and used it to test the countSubstrings method by passing the input values s and c. The countSubstrings method is the same as the given code, which counts the number of substrings in the string s that do not contain the character c.
Time Complexity: The time complexity of the given code is O(n), where n is the length of the string s. This is because we are traversing the string s only once using the end pointer and performing constant-time operations in the while loop.
Auxiliary Space: The auxiliary space required by the given code is O(1), which is constant space. This is because we are not using any additional data structures or arrays to store the intermediate results, and all the calculations are done using the existing variables.
Last Updated :
09 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...