Count of substrings formed using a given set of characters only
Given a string str and an array arr[] of K characters, the task is to find the number of substrings of str that contain characters only from the given character array arr[].
Note: The string str and the arr[] contain only lowercase alphabets.
Examples:
Input: S = “abcb”, K = 2, charArray[] = {‘a’, ‘b’}
Output: 4
Explanation:
The substrings are “a”, “ab”, “b”, “b” using the available characters
Input: S = “aabdbbtr”, K = 4, charArray[] = {‘e’, ‘a’, ‘r’, ‘t’}
Output: 6
Explanation:
The substrings “a”, “aa”, “a”, “t”, “tr”, “r” using the available characters.
Naive Approach: The naive approach is generate all possible substrings for the given string str and check if each substring consists of given characters in the array arr[] or not. If Yes then count that substring else check for the next substring.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The above naive approach can be optimized as we will delete the count of substrings formed by characters which are not present in the given array arr[]. Below are the steps:
- Store the characters present in the array arr[] in a boolean array of size 26, so that the searching of any character can be done in O(1) time.
- The total number of substrings formed by string of length N is (N*(N+1))/2, initialise count as (N*(N+1))/2.
- Traverse the string from left to right and store the index of the last character that we encountered in the string which is not present in the array arr[] using a variable lastPos
- If while traversing the string we encounter any character that is not present in arr[] then we subtract number of substrings that will contain this character and will have a starting point greater than the value of lastPos. Suppose we are at index i then the number of substrings to be subtracted will be given by
(i - lastPos)*(N - i)
- Update the value of lastPos everytime we encounter a character not available in charArray[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numberofsubstrings(string str, int k,
char charArray[])
{
int N = str.length();
bool available[26] = { 0 };
for ( int i = 0; i < k; i++) {
available[charArray[i] - 'a' ] = 1;
}
int lastPos = -1;
int ans = (N * (N + 1)) / 2;
for ( int i = 0; i < N; i++) {
if (available[str[i] - 'a' ] == 0) {
ans -= ((i - lastPos)
* (N - i));
lastPos = i;
}
}
cout << ans << endl;
}
int main()
{
string str = "abcb" ;
int k = 2;
char charArray[k] = { 'a' , 'b' };
numberofsubstrings(str, k, charArray);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
public static void numberofsubstrings(String str, int k,
char charArray[])
{
int N = str.length();
int available[] = new int [ 26 ];
Arrays.fill(available, 0 );
for ( int i = 0 ; i < k; i++)
{
available[charArray[i] - 'a' ] = 1 ;
}
int lastPos = - 1 ;
int ans = (N * (N + 1 )) / 2 ;
for ( int i = 0 ; i < N; i++)
{
if (available[str.charAt(i) - 'a' ] == 0 )
{
ans -= ((i - lastPos) * (N - i));
lastPos = i;
}
}
System.out.println(ans);
}
public static void main(String args[])
{
String str = "abcb" ;
int k = 2 ;
char []charArray = { 'a' , 'b' };
numberofsubstrings(str, k, charArray);
}
}
|
Python3
def numberofsubstrings( str , k, charArray):
N = len ( str )
available = [ 0 ] * 26
for i in range ( 0 , k):
available[ ord (charArray[i]) -
ord ( 'a' )] = 1
lastPos = - 1
ans = (N * (N + 1 )) / 2
for i in range ( 0 , N):
if (available[ ord ( str [i]) -
ord ( 'a' )] = = 0 ):
ans - = ((i - lastPos) * (N - i))
lastPos = i
print ( int (ans))
str = "abcb"
k = 2
charArray = [ 'a' , 'b' ]
numberofsubstrings( str , k, charArray)
|
C#
using System;
class GFG{
public static void numberofsubstrings(String str, int k,
char []charArray)
{
int N = str.Length;
int []available = new int [26];
for ( int i = 0; i < k; i++)
{
available[charArray[i] - 'a' ] = 1;
}
int lastPos = -1;
int ans = (N * (N + 1)) / 2;
for ( int i = 0; i < N; i++)
{
if (available[str[i] - 'a' ] == 0)
{
ans -= ((i - lastPos) * (N - i));
lastPos = i;
}
}
Console.WriteLine(ans);
}
public static void Main(String []args)
{
String str = "abcb" ;
int k = 2;
char []charArray = { 'a' , 'b' };
numberofsubstrings(str, k, charArray);
}
}
|
Javascript
<script>
function numberofsubstrings(str, k, charArray)
{
var N = str.length;
var available = [ 26 ];
for ( var i = 0; i < k; i++)
{
available[charArray[i] - 'a' ] = 1;
}
var lastPos = -1;
var ans = (N * (N + 1)) / 2;
for ( var i = 0; i < N; i++)
{
if (available[str.charAt(i) - 'a' ] == 0)
{
ans -= ((i - lastPos) * (N - i));
lastPos = i;
}
}
document.write(ans);
}
var str = "abcb" ;
var k = 2;
var charArray = [ 'a' , 'b' ];
numberofsubstrings(str, k, charArray);
</script>
|
Time Complexity: O(N), N is the length of the string
Auxiliary Space: O(1)
Last Updated :
09 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...