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 continuos 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.
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 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
- Maximum length palindromic substring such that it starts and ends with given char
- Find if a string starts and ends with another given string
- Ways to split string such that each partition starts with distinct character
- Number of sub-strings that contain the given character exactly k times
- Number of character corrections in the given strings to make them equal
- Check if strings are rotations of each other or not | Set 2
- Total character pairs from two strings, with equal number of set bits in their ascii value
- A Program to check if strings are rotations of each other or not
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Check if a number N starts with 1 in b-base
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Generate all rotations of a number
- Check whether the binary equivalent of a number ends with "001" or not
- Check whether the binary equivalent of a number ends with given string or not
- Count of sub-strings that contain character X at least once
- Sub-strings of length K containing same character