Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time
Given a string str consisting only of the characters ‘a’, ‘b’ and ‘c’, find the number of sub-strings that do not contain all the three characters at the same time.
Examples:
Input: str = “abc”
Output: 5
The possible substrings are “a”, “b”, “c”, “ab” and “bc”
Input: str = “babac”
Output: 12
Approach: The idea is to use three variables a_index, b_index and c_index to store the latest occurrence of the characters a, b and c and another variable ans to store the number of substrings that don’t have at least one of a, b or c and initialize its value to the total number of substrings with length n i.e, n*(n+1)/2, where n is the length of the string.
Now simply traverse the string from the beginning. At each point update the value of variables to the latest value whenever encountered.Since we are indexing with 0, so we are updating the index as i+1 at each step.
Also at each step, you need to find the index of minimum occurrence of the remaining of the two characters not encountered in the current step.
Then simply deduct this index from the variable ans. This is because once you found the index of the minimum of the remaining characters you are sure that any more substrings formed by moving downwards in index will also contain all the three characters.
Hence the total numbers of all such substrings(ones containing all a, b, and c) formed at this step is the found index.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountSubstring(string str, int n)
{
int ans = (n * (n + 1)) / 2;
int a_index = 0;
int b_index = 0;
int c_index = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == 'a' ) {
a_index = i + 1;
ans -= min(b_index, c_index);
}
else if (str[i] == 'b' ) {
b_index = i + 1;
ans -= min(a_index, c_index);
}
else {
c_index = i + 1;
ans -= min(a_index, b_index);
}
}
return ans;
}
int main()
{
string str = "babac" ;
int n = str.length();
cout << CountSubstring(str, n);
return 0;
}
|
Java
class GFG
{
static int CountSubstring( char str[], int n)
{
int ans = (n * (n + 1 )) / 2 ;
int a_index = 0 ;
int b_index = 0 ;
int c_index = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (str[i] == 'a' )
{
a_index = i + 1 ;
ans -= Math.min(b_index, c_index);
}
else if (str[i] == 'b' )
{
b_index = i + 1 ;
ans -= Math.min(a_index, c_index);
}
else
{
c_index = i + 1 ;
ans -= Math.min(a_index, b_index);
}
}
return ans;
}
public static void main(String[] args)
{
char str[] = "babac" .toCharArray();
int n = str.length;
System.out.println(CountSubstring(str, n));
}
}
|
Python3
def CountSubString( Str , n):
ans = (n * (n + 1 )) / / 2
a_index = 0
b_index = 0
c_index = 0
for i in range (n):
if ( Str [i] = = 'a' ):
a_index = i + 1
ans - = min (b_index, c_index)
elif ( Str [i] = = 'b' ):
b_index = i + 1
ans - = min (a_index, c_index)
else :
c_index = i + 1
ans - = min (a_index, b_index)
return ans
Str = "babac"
n = len ( Str )
print (CountSubString( Str , n))
|
C#
using System;
class GFG
{
static int CountSubstring( char []str, int n)
{
int ans = (n * (n + 1)) / 2;
int a_index = 0;
int b_index = 0;
int c_index = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] == 'a' )
{
a_index = i + 1;
ans -= Math.Min(b_index, c_index);
}
else if (str[i] == 'b' )
{
b_index = i + 1;
ans -= Math.Min(a_index, c_index);
}
else
{
c_index = i + 1;
ans -= Math.Min(a_index, b_index);
}
}
return ans;
}
public static void Main()
{
char []str = "babac" .ToCharArray();
int n = str.Length;
Console.WriteLine(CountSubstring(str, n));
}
}
|
PHP
<?php
function CountSubstring( $str , $n )
{
$ans = ( $n * ( $n + 1)) / 2;
$a_index = 0;
$b_index = 0;
$c_index = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] == 'a' )
{
$a_index = $i + 1;
$ans -= min( $b_index , $c_index );
}
else if ( $str [ $i ] == 'b' )
{
$b_index = $i + 1;
$ans -= min( $a_index , $c_index );
}
else
{
$c_index = $i + 1;
$ans -= min( $a_index , $b_index );
}
}
return $ans ;
}
{
$str = str_split ( "babac" );
$n = sizeof( $str );
echo (CountSubstring( $str , $n ));
}
|
Javascript
<script>
function CountSubstring(str, n) {
var ans = parseInt((n * (n + 1)) / 2);
var a_index = 0;
var b_index = 0;
var c_index = 0;
for ( var i = 0; i < n; i++) {
if (str[i] === "a" ) {
a_index = i + 1;
ans -= Math.min(b_index, c_index);
}
else if (str[i] === "b" ) {
b_index = i + 1;
ans -= Math.min(a_index, c_index);
}
else {
c_index = i + 1;
ans -= Math.min(a_index, b_index);
}
}
return ans;
}
var str = "babac" .split( "" );
var n = str.length;
document.write(CountSubstring(str, n));
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(1)
Last Updated :
05 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...