
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++
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”
Approach used in the below program is as follows
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.
Example
#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; }
Output
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
- Related Articles
- Count of sub-strings that contain character X at least once in C++
- Count all Palindrome Sub-Strings in a String in C++
- Count of sub-strings of length n possible from the given string in C++
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Count the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++
- Count characters at same position as in English alphabets in C++
- Count common characters in two strings in C++
- Find the count of sub-strings whose characters can be rearranged to form the given word in Python
- Check that the String does not contain certain characters in Java
- Count characters with same neighbors in C++
- Remove all the strings from the StringCollection in C#
- Is the weather of a particular place same at all the time?
- Get or set the number of elements that the ArrayList can contain in C#
- Count strings that end with the given pattern in C++
