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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count substrings with same first and last characters
- Count substrings with different first and last characters
- Count number of substrings of a string consisting of same characters
- Count of substrings of a given Binary string with all characters same
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Maximal Clique Problem | Recursive Solution
- Minimum operation require to make first and last character same
- Java program to swap first and last characters of words in a sentence
- Program to check if first and the last characters of string are equal
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Print the last k nodes of the linked list in reverse order | Recursive approach
- String with k distinct characters and no same characters adjacent
- Count substrings of same length differing by a single character from two given strings
- Check whether two strings contain same characters in same order
- Count number of substrings with exactly k distinct 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 of substrings of length K with exactly K distinct characters
- Count of substrings formed using a given set of characters only