# Count of Substrings that can be formed without using the given list of Characters

Given a string **str** and a list of characters **L**, the task is to count total numbers of substrings of the string **str** without using characters given in the list **L**.

**Examples:**

Input:str = “abcd”, L[] = {‘a’, ‘b’, ‘t’, ‘q’}

Output:3

Explanation:

On ignoring characters ‘a’ and ‘b’ from the given string, substring “cd” is left.

Therefore, the total number of substrings formed with “cd” are:

(2 * (2 + 1)) / 2 = 3

Input:str = “abcpxyz”, L[] = {‘a’, ‘p’, ‘q’}

Output:9

Explanation:

On ignoring characters ‘a’ and ‘p’ from the given string, substrings “bc” and “xyz” are left.

Therefore, total number of substrings formed with the substrings are:

(2*(2+1))/2 + (3*(3+1))/2 = 3 + 6 = 9

**Approach:** The total number of substrings for a given string of length N is given by the formula

(N * (N + 1)) / 2

The idea is to use the above formula and follow the below steps to compute the answer:

- Traverse the string
**str**character by character. - Count the number of characters till which a character from list L is not found. Let the count be
**N** - Once a character from
**L**is encountered, compute**(N * (N + 1) / 2)**and add it to the**answer**and reset count N to zero.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the Number of sub-strings ` `// without using given character ` `int` `countSubstring(string& S, ` `char` `L[], ` `int` `& n) ` `{ ` ` ` `int` `freq[26] = { 0 }, ans = 0; ` ` ` ` ` `// Mark the given characters in ` ` ` `// the freq array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `freq[(` `int` `)(L[i] - ` `'a'` `)] = 1; ` ` ` `} ` ` ` ` ` `// Count variable to store the count ` ` ` `// of the characters until a character ` ` ` `// from given L is encountered ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `auto` `x : S) { ` ` ` ` ` `// If a character from L is encountered, ` ` ` `// then the answer variable is incremented by ` ` ` `// the value obtained by using ` ` ` `// the mentioned formula and count is set to 0 ` ` ` `if` `(freq[(` `int` `)(x - ` `'a'` `)]) { ` ` ` `ans += (count * count + count) / 2; ` ` ` `count = 0; ` ` ` `} ` ` ` `else` ` ` `count++; ` ` ` `} ` ` ` ` ` `// For last remaining characters ` ` ` `ans += (count * count + count) / 2; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `string S = ` `"abcpxyz"` `; ` ` ` `char` `L[] = { ` `'a'` `, ` `'p'` `, ` `'q'` `}; ` ` ` `int` `n = ` `sizeof` `(L) / ` `sizeof` `(L[0]); ` ` ` ` ` `cout << countSubstring(S, L, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the Number of sub-Strings ` `// without using given character ` `static` `int` `countSubString(` `char` `[]S, ` `char` `L[], ` `int` `n) ` `{ ` ` ` `int` `[]freq = ` `new` `int` `[` `26` `]; ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Mark the given characters in ` ` ` `// the freq array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `freq[(` `int` `)(L[i] - ` `'a'` `)] = ` `1` `; ` ` ` `} ` ` ` ` ` `// Count variable to store the count ` ` ` `// of the characters until a character ` ` ` `// from given L is encountered ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `for` `(` `int` `x : S) ` ` ` `{ ` ` ` ` ` `// If a character from L is encountered, ` ` ` `// then the answer variable is incremented by ` ` ` `// the value obtained by using ` ` ` `// the mentioned formula and count is set to 0 ` ` ` `if` `(freq[(` `int` `)(x - ` `'a'` `)] > ` `0` `) ` ` ` `{ ` ` ` `ans += (count * count + count) / ` `2` `; ` ` ` `count = ` `0` `; ` ` ` `} ` ` ` `else` ` ` `count++; ` ` ` `} ` ` ` ` ` `// For last remaining characters ` ` ` `ans += (count * count + count) / ` `2` `; ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String S = ` `"abcpxyz"` `; ` ` ` `char` `L[] = { ` `'a'` `, ` `'p'` `, ` `'q'` `}; ` ` ` `int` `n = L.length; ` ` ` ` ` `System.out.print(countSubString(S.toCharArray(), L, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function to find the Number of sub-strings ` `# without using given character ` `def` `countSubstring(S, L,n): ` ` ` `freq ` `=` `[` `0` `for` `i ` `in` `range` `(` `26` `)] ` ` ` ` ` `# the freq array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `freq[(` `ord` `(L[i]) ` `-` `ord` `(` `'a'` `))] ` `=` `1` ` ` ` ` `# Count variable to store the count ` ` ` `# of the characters until a character ` ` ` `# from given L is encountered ` ` ` `count,ans ` `=` `0` `,` `0` ` ` ` ` `for` `x ` `in` `S: ` ` ` ` ` `# If a character from L is encountered, ` ` ` `# then the answer variable is incremented by ` ` ` `# the value obtained by using ` ` ` `# the mentioned formula and count is set to 0 ` ` ` `if` `(freq[` `ord` `(x) ` `-` `ord` `(` `'a'` `)]): ` ` ` `ans ` `+` `=` `(count ` `*` `count ` `+` `count) ` `/` `/` `2` ` ` `count ` `=` `0` ` ` `else` `: ` ` ` `count ` `+` `=` `1` ` ` ` ` `# For last remaining characters ` ` ` `ans ` `+` `=` `(count ` `*` `count ` `+` `count) ` `/` `/` `2` ` ` ` ` `return` `ans ` ` ` `# Driver code ` ` ` `S ` `=` `"abcpxyz"` `L ` `=` `[` `'a'` `, ` `'p'` `, ` `'q'` `] ` `n ` `=` `len` `(L) ` ` ` `print` `(countSubstring(S, L, n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the Number of sub-Strings ` ` ` `// without using given character ` ` ` `static` `int` `countSubString(` `char` `[]S, ` `char` `[]L, ` `int` `n) ` ` ` `{ ` ` ` `int` `[]freq = ` `new` `int` `[26]; ` ` ` `int` `ans = 0; ` ` ` ` ` `// Mark the given characters in ` ` ` `// the freq array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `freq[(` `int` `)(L[i] - ` `'a'` `)] = 1; ` ` ` `} ` ` ` ` ` `// Count variable to store the count ` ` ` `// of the characters until a character ` ` ` `// from given L is encountered ` ` ` `int` `count = 0; ` ` ` ` ` `foreach` `(` `int` `x ` `in` `S) ` ` ` `{ ` ` ` ` ` `// If a character from L is encountered, ` ` ` `// then the answer variable is incremented by ` ` ` `// the value obtained by using ` ` ` `// the mentioned formula and count is set to 0 ` ` ` `if` `(freq[(` `int` `)(x - ` `'a'` `)] > 0) ` ` ` `{ ` ` ` `ans += (count * count + count) / 2; ` ` ` `count = 0; ` ` ` `} ` ` ` `else` ` ` `count++; ` ` ` `} ` ` ` ` ` `// For last remaining characters ` ` ` `ans += (count * count + count) / 2; ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `string` `S = ` `"abcpxyz"` `; ` ` ` `char` `[]L = { ` `'a'` `, ` `'p'` `, ` `'q'` `}; ` ` ` `int` `n = L.Length; ` ` ` ` ` `Console.WriteLine(countSubString(S.ToCharArray(), L, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

9

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Count substrings with same first and last characters
- Count number of substrings with exactly k distinct characters
- Count of all unique substrings with non-repeating characters
- Recursive solution to count substrings with same first and last characters
- Count distinct substrings that contain some characters at most k times
- Count number of substrings of a string consisting of same characters
- Count of times second string can be formed from the characters of first string
- Strings formed from given characters without any consecutive repeating characters
- Get K-th letter of the decoded string formed by repeating substrings
- Queries for frequencies of characters in substrings
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- K length words that can be formed from given characters without repetition
- Lexicographically largest string formed from the characters in range L and R
- Check if a given string can be formed using characters of adjacent cells of a Matrix

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.