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

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// required sub-strings ` `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 code ` `int` `main() ` `{ ` ` ` `string str = ` `"abcabc"` `; ` ` ` `int` `n = str.length(); ` ` ` `char` `x = ` `'c'` `; ` ` ` ` ` `cout << countSubStr(str, n, x); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// required sub-strings ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` ` ` `# Function to return the count of ` `# required sub-strings ` `def` `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 code ` `str` `=` `"abcabc"` `; ` `n ` `=` `len` `(` `str` `); ` `x ` `=` `'c'` `; ` ` ` `print` `(countSubStr(` `str` `, n, x)); ` ` ` `# This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// required sub-strings ` `static` `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 code ` `public` `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 */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count of ` `// required sub-strings ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

15

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count substrings that starts with character X and ends with character Y
- Minimize length of Substrings containing at least one common Character
- Count distinct substrings that contain some characters at most k times
- Count substrings that contain all vowels | SET 2
- Count of strings that can be formed from another string using each character at-most once
- Modify the string such that it contains all vowels at least once
- Find substrings that contain all vowels
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Number of substrings with count of each character as k
- Count substrings with each character occurring at most k times
- Count of substrings which contains a given character K times
- Count all substrings having character K
- Count of substrings containing only the given character
- Number of sub-strings that contain the given character exactly k times
- Check if a String contains Anagrams of length K which does not contain the character X
- Print Kth character in sorted concatenated substrings of a string
- Maximize partitions such that no two substrings have any common character
- Longest subsequence where every character appears at-least k times
- Lexicographically largest subsequence such that every character occurs at least k times

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.