Given a string str, the task is to find the number of strings that start and end with the same character after a rotation at every possible index of the given string.
Input: str = “GeeksforGeeks”
All possible strings with rotations at every index are: “GeeksforGeeks”, “eeksforGeeksG”, “eksforGeeksGe”, “ksforGeeksGee”, “sforGeeksGeek”, “forGeeksGeeks”, “orGeeksGeeksf”, “rGeeksGeeksfo”, “GeeksGeeksfor”, “eeksGeeksforG”, “eksGeeksforGe”, “ksGeeksforGee”, “sGeeksforGeek”.
Out of the above strings formed only 2 string starts and ends with the same characters: “eksforGeeksGe” and “eksGeeksforGe”.
Input: str = “aaabcdd”
All possible strings with rotations at every index are: “aaabcdd”, “aabcdda”, “abcddaa”, “bcddaaa”, “cddaaab”, “ddaaabc”, “daaabcd”.
Out of the above strings formed only 3 string starts and ends with the same characters: “aabcdda”, “abcddaa” and “daaabcd”.
Naive Approach: The idea is to generate all the possible rotations of the given string and check whether the each string formed after rotation starts and ends with the same character or not. If Yes then include this string in the count. Print the final count.
Efficient Approach: The efficient approach to counting the possible string is to rotate the given string at those indexes which have continuous same characters. Therefore, the final count is the (number of continuous same characters – 1) for each continuous characters in the given string.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the given string.
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 that starts with character X and ends with character Y
- Build a DFA to accept Binary strings that starts or ends with "01"
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Find if a string starts and ends with another given string
- Maximum length palindromic substring such that it starts and ends with given char
- Longest substring that starts with X and ends with Y
- Meta Strings (Check if two strings can become same after a swap in one string)
- Ways to split string such that each partition starts with distinct character
- Count rotations of N which are Odd and Even
- Minimum rotations required to get the same string
- Minimum rotations required to get the same String | Set-2
- Maximum contiguous 1 possible in a binary string after k rotations
- Find the Mth element of the Array after K left rotations
- Mth element after K Right Rotations of an Array
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Program to build a DFA to accept strings that start and end with same character
- Count rotations which are divisible by 10
- Check if a number starts with another number or not
- A Program to check if strings are rotations of each other or not
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.