Count of sub-strings that contain character X at least once - GeeksforGeeks

07 Jun, 2021
Given a string str and a character X. The task is to find the total number of sub-strings that contain the character X at least once.Examples:  
Input: str = “abcd”, X = ‘b’ Output: 6 “ab”, “abc”, “abcd”, “b”, “bc” and “bcd” are the required sub-strings.Input: str = “geeksforgeeks”, X = ‘e’ Output: 66  
 
Approach: Total number of sub-strings are n * (n + 1) / 2, among them only those sub-strings need to be counted which contain character X. Character X is present in those sub-strings from position of X to the length of the string. For each character before X this sub-string must be counted.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of// required sub-stringsint countSubStr(string str, int n, char x){    int res = 0, count = 0;    for (int i = 0; i < n; i++) {        if (str[i] == x) {             // Number of sub-strings from position            // of current x to the end of str            res += ((count + 1) * (n - i));             // To store the number of characters            // before x            count = 0;        }        else            count++;    }     return res;} // Driver codeint main(){    string str = "abcabc";    int n = str.length();    char x = 'c';     cout << countSubStr(str, n, x);     return 0;}
// Java implementation of the approachclass GFG{ // Function to return the count of// required sub-stringsstatic int countSubStr(String str, int n, char x){    int res = 0, count = 0;    for (int i = 0; i < n; i++)    {        if (str.charAt(i) == x)        {             // Number of sub-strings from position            // of current x to the end of str            res += ((count + 1) * (n - i));             // To store the number of characters            // before x            count = 0;        }        else            count++;    }     return res;} // Driver codepublic static void main(String[] args){    String str = "abcabc";    int n = str.length();    char x = 'c';     System.out.println(countSubStr(str, n, x));}} // This code has been contributed by 29AjayKumar
# Python implementation of the approach  # Function to return the count of# required sub-stringsdef countSubStr(str, n, x):    res = 0; count = 0;    for i in range(n):        if (str[i] == x):              # Number of sub-strings from position            # of current x to the end of str            res += ((count + 1) * (n - i));              # To store the number of characters            # before x            count = 0;        else:            count+=1;      return res; # Driver codestr = "abcabc";n = len(str);x = 'c';  print(countSubStr(str, n, x)); # This code contributed by PrinciRaj1992
// C# implementation of the approachusing System; class GFG{ // Function to return the count of// required sub-stringsstatic int countSubStr(String str, int n, char x){    int res = 0, count = 0;    for (int i = 0; i < n; i++)    {        if (str[i] == x)        {             // Number of sub-strings from position            // of current x to the end of str            res += ((count + 1) * (n - i));             // To store the number of characters            // before x            count = 0;        }        else            count++;    }     return res;} // Driver codepublic static void Main(String[] args){    String str = "abcabc";    int n = str.Length;    char x = 'c';     Console.WriteLine(countSubStr(str, n, x));}} /* This code contributed by PrinciRaj1992 */
<?php// PHP implementation of the approach // Function to return the count of// required sub-stringsfunction countSubStr($str, $n, $x){    $res = 0; $count = 0;    for ($i = 0; $i < $n; $i++)    {        if ($str[$i] == $x)        {             // Number of sub-strings from position            // of current x to the end of str            $res += (($count + 1) * ($n - $i));             // To store the number of characters            // before x            $count = 0;        }        else            $count++;    }     return $res;} // Driver code$str = "abcabc";$n = strlen($str);$x = 'c'; echo countSubStr($str, $n, $x); // This code is contributed by Ryuga?>
<script>    // Javascript implementation of the approach         // Function to return the count of    // required sub-strings    function countSubStr(str, n, x)    {        let res = 0, count = 0;        for (let i = 0; i < n; i++)        {            if (str[i] == x)            {                 // Number of sub-strings from position                // of current x to the end of str                res += ((count + 1) * (n - i));                 // To store the number of characters                // before x                count = 0;            }            else                count++;        }         return res;    }         let str = "abcabc";    let n = str.length;    let x = 'c';       document.write(countSubStr(str, n, x)); // This code is contributed by divyeshrabadiya07.</script>
15
 
ankthon
29AjayKumar
princiraj1992
divyeshrabadiya07
substring
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Array of Strings in C++ (5 Different Ways to Create)
Different methods to reverse a string in C/C++
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews