Given a string str and a character c. The task is to find the number of sub-strings that do not consist of the character c.
Input: str = “baa”, c = ‘b’
The sub-strings are “a”, “a” and “aa”
Input: str = “ababaa”, C = ‘b’
Approach: Initially take a counter that counts the number of characters continuously with no character c. Iterate in the string and increase the counter till str[i] != c. Once str[i] == c, the number of sub-strings from the contiguous length cnt will be (cnt * (cnt + 1)) / 2. After the complete traversal of the string also add (cnt *(cnt + 1)) / 2 to the result for the group of characters appearing after the last occurrence of c.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count substrings that starts with character X and ends with character Y
- Count all substrings having character K
- Count of substrings which contains a given character K times
- Number of substrings with count of each character as k
- Count substrings with each character occurring at most k times
- Maximize partitions such that no two substrings have any common character
- Print Kth character in sorted concatenated substrings of a string
- Find the count of M character words which have at least one character repeated
- Count substrings that contain all vowels | SET 2
- Count substrings with same first and last characters
- Count of substrings of a binary string containing K ones
- Count of substrings consisting only of vowels
- Count of substrings having all distinct characters
- Count of total anagram substrings
- Count Substrings with equal number of 0s, 1s and 2s
- Count of substrings formed using a given set of characters only
- Find the count of substrings in alphabetic order
- Count number of distinct substrings of a given length
- Count distinct substrings that contain some characters at most k times
- Count all Prime Length Palindromic Substrings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.