- Related Questions & Answers
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- Convert all substrings of length ‘k’ from base ‘b’ to decimal in C++
- What is the use of ‘ALL’, ‘ANY’, ’SOME’, ’IN’ operators with MySQL subquery?
- K-th digit in ‘a’ raised to power ‘b’ in C++
- Find all the names beginning with the letter 'a' or ‘b’ or ‘c’ using MySQL query?
- What is the benefit of MySQL ‘IS NULL’ and ‘IS NOT NULL’?
- Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’
- Are ‘this’ and ‘super’ keywords in Java?
- ‘AND’ vs ‘&&’ operators in PHP
- Count of sub-strings that do not contain all the in C++
- ‘this’ keyword in C#
- Why in MySQL, we cannot use arithmetic operators like ‘=’, ‘<’ or ‘<>’ with NULL?
- What is the significance of ‘^’ in PHP?
- What is the use of ‘new’ keyword in C#?
- What is the use of ‘Using’ statement in C#?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given a string str[] containing ‘a’, ‘b’ and ‘c’ only. The goal is to find the substrings of str[] such that all the three characters are not part of that substring. For any string str, substrings could be “a”, “b”, “c”, “abb”, “bba”, “bc”, “ca”, “ccc” but not “abc”, “bcca” , “cab” as these have ‘a’, ‘b’ and ‘c’, all three.

Let us understand with examples.

**Input** − str[] = “aabc”

**Output** − Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are − 8

**Explanation** − Substrings will be : “a”, “a”, “b”, “c”, “aa”, “ab”, “bc”, “aab”

**Input** − str[] = “abcabc”

**Output** − Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are − 11

**Explanation** − Substrings will be : “a”, “b”, “c”, “a”, “b”, “c”, “ab”, “bc”, “ca”, “ab”, “bc”

In this approach we know that total number of substrings of string with n characters is n*(n+1)/2.

We will now traverse the string and for each type of character ‘a’, ‘b’ or ‘c’. We will check for previous indexes of the other two characters (‘b’,’c’), (‘c’,’a’) and (‘a’, ‘b’). Just subtract the minimum index of the other two from count as we know that we are removing that character to include current character in substring so that it does not contain all three.

Take a string str as a character array.

Function sub_without_all(char str[], int size) takes a string, it’s length and returns the count of substrings that do not contain ‘a’, ‘b’ and ‘c’ all together.

Take the initial count size*(size+1)/2 for number of all possible substrings of str[].

Take variables a,b,c to store the last index of ‘a’, ‘b’, ‘c’ in str[]. Initialize all with 0.

Traverse str[] using for loop from i=0 to i<size.

If str[i]==’a’ update index of ‘a’ as a=i+1. Subtract minimum of index of ‘b’ or ‘c’ from count to include ‘a’ in substring. Subtract b,c whichever is minimum from count.

Do similar as the previous step for str[i]==’b’ or str[i]==’c’.

At the end we have count as substrings of str[] without all three characters in them at once.

Return count as result.

#include <bits/stdc++.h> using namespace std; int sub_without_all(char str[], int size){ int update_size = size * (size + 1); int count = update_size / 2; int a, b, c; a = b = c = 0; for (int i = 0; i < size; i++){ if (str[i] == 'a'){ a = i + 1; count -= min(b, c); } else if (str[i] == 'b'){ b = i + 1; count -= min(a, c); } else{ c = i + 1; count -= min(a, b); } } return count; } int main(){ char str[] = "abcabbc"; int size = strlen(str); cout<<"Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are: "<<sub_without_all(str, size); return 0; }

If we run the above code it will generate the following output −

Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are: 15

Advertisements