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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count substrings with same first and last characters
- Count distinct substrings that contain some characters at most k times
- Count of Substrings that can be formed without using the given list of Characters
- Count of all unique substrings with non-repeating characters
- Count number of substrings with exactly k distinct 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
- 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 Substrings with equal number of 0s, 1s and 2s
- Count of total anagram substrings