Given a string str of n lowercase characters, the task is to count the number of substrings of str starting with character X and ending with character Y.
Input: str = "abbcaceghcak" x = 'a', y = 'c' Output: 5 abbc, abbcac, ac, abbcaceghc, aceghc Input: str = "geeksforgeeks" x = 'g', y = 'e' Output: 6
- Initialize two counters i.e. tot_count to count the total number of substrings and count_x to count the number of strings that start with X.
- Start traversing the string.
- If the current character is X then increment the count of count_x.
- If the current character is Y, it means a string ends at Y so increment the count of tot_count i.e.
tot_count = tot_count + count_x
It means that if there exists a Y then it will make a substring with all the X occurs before Y in the string. So, add the count of X to the total count.
- Return total count.
Below is the implementation of above approach:
Count = 5
- Return maximum occurring character in an input string
- Given a string, find its first non-repeating character
- Count words in a given string
- Find the first non-repeating character from a stream of characters
- Given a binary string, count number of substrings that start and end with 1.
- K'th Non-repeating Character
- Longest Common Prefix using Character by Character Matching
- Count number of substrings with exactly k distinct characters
- Find k'th character of decrypted string | Set 1
- Count Distinct Subsequences
- Count Substrings with equal number of 0s, 1s and 2s
- Count of distinct substrings of a string using Suffix Trie
- Remove a character from a string to make it a palindrome
- Count characters at same position as in English alphabet
- Check for Palindrome after every character replacement Query
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.