Count of Substrings that can be formed without using the given list of Characters
Last Updated :
21 Dec, 2021
Given a string str and a list of characters L, the task is to count the 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 the characters ‘a’ and ‘b’ from the given string, substring “cd” is left.
Therefore, the total number of substrings formed by “cd” by:
(2 * (2 + 1)) / 2 = 3
Input: str = “abcpxyz”, L[] = {‘a’, ‘p’, ‘q’}
Output: 9
Explanation:
On ignoring the characters ‘a’ and ‘p’ from the given string, substrings “bc” and “xyz” are left.
Therefore, the total number of substrings formed by the substrings is:
(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 on 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 the count N to zero.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubstring(string& S, char L[], int & n)
{
int freq[26] = { 0 }, ans = 0;
for ( int i = 0; i < n; i++) {
freq[( int )(L[i] - 'a' )] = 1;
}
int count = 0;
for ( auto x : S) {
if (freq[( int )(x - 'a' )]) {
ans += (count * count + count) / 2;
count = 0;
}
else
count++;
}
ans += (count * count + count) / 2;
return ans;
}
int main()
{
string S = "abcpxyz" ;
char L[] = { 'a' , 'p' , 'q' };
int n = sizeof (L) / sizeof (L[0]);
cout << countSubstring(S, L, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countSubString( char []S, char L[], int n)
{
int []freq = new int [ 26 ];
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
freq[( int )(L[i] - 'a' )] = 1 ;
}
int count = 0 ;
for ( int x : S)
{
if (freq[( int )(x - 'a' )] > 0 )
{
ans += (count * count + count) / 2 ;
count = 0 ;
}
else
count++;
}
ans += (count * count + count) / 2 ;
return ans;
}
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));
}
}
|
Python3
def countSubstring(S, L,n):
freq = [ 0 for i in range ( 26 )]
for i in range (n):
freq[( ord (L[i]) - ord ( 'a' ))] = 1
count,ans = 0 , 0
for x in S:
if (freq[ ord (x) - ord ( 'a' )]):
ans + = (count * count + count) / / 2
count = 0
else :
count + = 1
ans + = (count * count + count) / / 2
return ans
S = "abcpxyz"
L = [ 'a' , 'p' , 'q' ]
n = len (L)
print (countSubstring(S, L, n))
|
C#
using System;
class GFG
{
static int countSubString( char []S, char []L, int n)
{
int []freq = new int [26];
int ans = 0;
for ( int i = 0; i < n; i++)
{
freq[( int )(L[i] - 'a' )] = 1;
}
int count = 0;
foreach ( int x in S)
{
if (freq[( int )(x - 'a' )] > 0)
{
ans += (count * count + count) / 2;
count = 0;
}
else
count++;
}
ans += (count * count + count) / 2;
return ans;
}
public static void Main()
{
string S = "abcpxyz" ;
char []L = { 'a' , 'p' , 'q' };
int n = L.Length;
Console.WriteLine(countSubString(S.ToCharArray(), L, n));
}
}
|
Javascript
<script>
function countSubString(S, L, n) {
var freq = new Array(26).fill(0);
var ans = 0;
for ( var i = 0; i < n; i++) {
freq[L[i].charCodeAt(0) - "a" .charCodeAt(0)] = 1;
}
var count = 0;
for (const x of S) {
if (freq[x.charCodeAt(0) - "a" .charCodeAt(0)] > 0) {
ans = ans + parseInt((count * count + count) / 2);
count = 0;
} else count++;
}
ans += parseInt((count * count + count) / 2);
return ans;
}
var S = "abcpxyz" ;
var L = [ "a" , "p" , "q" ];
var n = L.length;
document.write(countSubString(S.split( "" ), L, n));
</script>
|
Time Complexity: O(n + |S|)
Auxiliary Space: O(26)
Share your thoughts in the comments
Please Login to comment...