We are given a string S, we need to find count of all contiguous substrings starting and ending with same character.
Input : S = "abcab" Output : 7 There are 15 substrings of "abcab" a, ab, abc, abca, abcab, b, bc, bca bcab, c, ca, cab, a, ab, b Out of the above substrings, there are 7 substrings : a, abca, b, bcab, c, a and b. Input : S = "aba" Output : 4 The substrings are a, b, a and aba
We have discussed different solutions in below post.
In this article, a simple recursive solution is discussed.
The time complexity of above solution is exponential. In Worst case, we may end up doing O(3n) operations.
This article is contributed by Yash Singla. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 with same first and last characters
- Count of substrings formed using a given set of characters only
- Count number of substrings with exactly k distinct characters
- Count of all unique substrings with non-repeating characters
- Count distinct substrings that contain some characters at most k times
- Count of substrings of length K with exactly K distinct characters
- Count of Substrings that can be formed without using the given list of Characters
- Count number of substrings of a string consisting of same characters
- Maximal Clique Problem | Recursive Solution
- Queries for frequencies of characters in substrings
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Remove minimum characters from string to split it into three substrings under given constraints
- Recursive program to insert a star between pair of identical characters
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count substrings that contain all vowels | SET 2
- Count all substrings having character K
- Count of total anagram substrings
- Count of substrings which contains a given character K times
- Count of substrings of a binary string containing K ones
- Count of substrings consisting only of vowels