
- 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 contain character X at least once in C++
We are given a string str[] and a character X. The goal is to find the substrings of str[] such that all the substrings contain X at least once. For str[]=”abc '' and X=’a’, the substrings containing ‘a’ at-least once are “a”, “ab”, “abc”. The count is 3.
Let us understand with examples.
Input − str[] = “aabccd” X=’c’
Output − Count of sub-strings that contain character X at least once are − 14
Explanation − Substrings containing at-least one ‘c’ will be : “c”, “c”, “bc”, “cc”, “cd”, “abc”, “bcc”, “ccd”, “aabc”, “abcc”, “bccd”, “aabcc”, “abccd”, “aabccd”.
Input − str[] = “settings” X=’s’
Output − Count of sub-strings that contain character X at least once are − 14
Explanation − Substrings contains at-least one ‘s’ will be : “s”, “s”, “se”, “gs”, “set”, “ngs”, “sett”, “ings”, “setti”, “tings”, “settin”, “ttings”, “setting”, “ettings”, “settings”
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 count characters before character X as temp. As soon as X is encountered, strings containing X will have length temp+1. Now we have X number of substrings containing X will be the remaining characters (length-current index) X ( temp+1). Add this to count. Now update temp=0 and proceed for next X till end of string. At the end we have count as the number of sub-strings that contain character X at least once.
Take a string str and a character x.
Function sub_x(char str[],int length,char x) takes a string, it’s length, character x and returns the count of substrings that contain character x at least once.
Take the initial count as 0. Take temp as characters before first x in str[] as 0 initially.
Take the initial count size*(size+1)/2 for number of all possible substrings of str[].
Traverse str[] using for loop from i=0 to i<size.
If str[i] is not x then increment temp as characters before first x.
If str[i] == x then length of string including x will be temp+1. Remaining characters of str[] will be length-i.
All substrings will be ( temp+1) * (length-i). Add this to count. Now update temp=0 for next iteration.
Do this until the end of str[] is reached.
Return count as result at the end.
Example
#include <bits/stdc++.h> using namespace std; int sub_x(string str, int length, char x){ int count = 0; int temp = 0; for (int i = 0; i < length; i++){ if (str[i] == x){ int temp_2 = temp + 1; count = count + temp_2 * (length - i); temp = 0; } else{ temp++; } } return count; } int main(){ string str = "abcabbc"; int length = str.length(); char x = 'a'; cout<<"Count of sub-strings that contain character X at least once are: "<<sub_x(str, length, x); return 0; }
Output
If we run the above code it will generate the following output −
Count of sub-strings that contain character X at least once are: 19
- Related Articles
- Count of strings that can be formed from another string using each character at-most once in C++
- Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++
- How to remove rows that contain at least one 0 in R?
- Program to find length of longest substring with character count of at least k in Python
- Common Character Count in Strings in JavaScript
- Numbers that are Bitwise AND of At Least One Non-Empty Sub-Array using C++
- Count all Palindrome Sub-Strings in a String in C++
- Count substrings that starts with character X and ends with character Y in C++
- Count divisors of n that have at-least one digit common with n in Java
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Count of sub-strings of length n possible from the given string in C++
- Get at least x number of rows in MySQL?
- A die is thrown twice. What is the probability that 5 will come up at least once?
- Count elements that are divisible by at-least one element in another array in C++
