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.
- Count substrings with same first and last characters
- Count number of substrings with exactly k distinct characters
- Count distinct substrings that contain some characters at most k times
- Count number of substrings of a string consisting of same characters
- 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 substrings that contain all vowels | SET 2
- Count of substrings which contains a given character K times
- Count Substrings with equal number of 0s, 1s and 2s
- Number of substrings with count of each character as k
- Count of substrings of a binary string containing K ones
- Count of total anagram substrings
- Find the count of substrings in alphabetic order
- Count number of substrings with numeric value greater than X