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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the number
// of sub-strings that do not contain
// the given character c
int countSubstrings(string s, char c)
{
  
    // Length of the string
    int n = s.length();
  
    int cnt = 0;
    int sum = 0;
  
    // Traverse in the string
    for (int i = 0; i < n; i++) {
  
        // If current character is different
        // from the given character
        if (s[i] != c)
            cnt++;
        else {
  
            // Update the number of sub-strings
            sum += (cnt * (cnt + 1)) / 2;
  
            // Reset count to 0
            cnt = 0;
        }
    }
  
    // For the characters appearing
    // after the last occurrence of c
    sum += (cnt * (cnt + 1)) / 2;
    return sum;
}
  
// Driver code
int main()
{
    string s = "baa";
    char c = 'b';
    cout << countSubstrings(s, c);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function to return the number
// of sub-strings that do not contain
// the given character c
static int countSubstrings(String s, char c)
{
  
    // Length of the string
    int n = s.length();
  
    int cnt = 0;
    int sum = 0;
  
    // Traverse in the string
    for (int i = 0; i < n; i++)
    {
  
        // If current character is different
        // from the given character
        if (s.charAt(i) != c)
            cnt++;
        else
        {
  
            // Update the number of sub-strings
            sum += (cnt * (cnt + 1)) / 2;
  
            // Reset count to 0
            cnt = 0;
        }
    }
  
    // For the characters appearing
    // after the last occurrence of c
    sum += (cnt * (cnt + 1)) / 2;
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    String s = "baa";
    char c = 'b';
    System.out.println(countSubstrings(s, c));
}
}
  
// This code is contributed by Code_Mech.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the number
# of sub-strings that do not contain
# the given character c
def countSubstrings(s, c):
  
    # Length of the string
    n = len(s)
  
    cnt = 0
    Sum = 0
  
    # Traverse in the string
    for i in range(n):
  
        # If current character is different
        # from the given character
        if (s[i] != c):
            cnt += 1
        else:
  
            # Update the number of sub-strings
            Sum += (cnt * (cnt + 1)) // 2
  
            # Reset count to 0
            cnt = 0
          
    # For the characters appearing
    # after the last occurrence of c
    Sum += (cnt * (cnt + 1)) // 2
    return Sum
  
# Driver code
s = "baa"
c = 'b'
print(countSubstrings(s, c))
  
# This code is contributed 
# by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the number
// of sub-strings that do not contain
// the given character c
static int countSubstrings(string s, char c)
{
  
    // Length of the string
    int n = s.Length;
  
    int cnt = 0;
    int sum = 0;
  
    // Traverse in the string
    for (int i = 0; i < n; i++)
    {
  
        // If current character is different
        // from the given character
        if (s[i] != c)
            cnt++;
        else
        {
  
            // Update the number of sub-strings
            sum += (cnt * (cnt + 1)) / 2;
  
            // Reset count to 0
            cnt = 0;
        }
    }
  
    // For the characters appearing
    // after the last occurrence of c
    sum += (cnt * (cnt + 1)) / 2;
    return sum;
}
  
// Driver code
public static void Main()
{
    string s = "baa";
    char c = 'b';
    Console.Write(countSubstrings(s, c));
}
}
  
// This code is contributed by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to return the number 
// of sub-strings that do not contain 
// the given character c 
function countSubstrings($s, $c
  
    // Length of the string 
    $n = strlen($s); 
  
    $cnt = 0; 
    $sum = 0; 
  
    // Traverse in the string 
    for ($i = 0; $i < $n; $i++) 
    
  
        // If current character is different 
        // from the given character 
        if ($s[$i] != $c
            $cnt++; 
        else
        
  
            // Update the number of sub-strings 
            $sum += floor(($cnt * ($cnt + 1)) / 2); 
  
            // Reset count to 0 
            $cnt = 0; 
        
    
  
    // For the characters appearing 
    // after the last occurrence of c 
    $sum += floor(($cnt * ($cnt + 1)) / 2); 
    return $sum
  
// Driver code 
$s = "baa"
$c = 'b'
  
echo countSubstrings($s, $c); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

3


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.