Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same.
Input: S = “abcd”
String after the left shift: “bcda”
String after the right shift: “dabc”
Changing the character at position 3 to ‘a’ and character at position 4 to ‘b’, the string is modified to “abab”.
Therefore, both the left and right rotations becomes “baba”.
Input: S = “gfg”
After updating the character at position 1 to ‘g’, the string becomes “ggg”.
Therefore, the left and right rotation are equal.
Approach: The key observation to solve the problem is that when the length of the string is even, then all the characters at even index and characters at odd index must be the same for the left and right rotations to be the same. For strings of odd length, all the characters must be equal. Follow the steps below to solve the problem:
- Check if the length of the string is even, then the minimum number of characters to be changed is the length of the string excluding the frequency of the most occurring element at the even indices and odd indices.
- Otherwise, if the length of the string is odd, then the minimum number of characters to be changed is the length of the string excluding the frequency of the most occurring character in the string.
- Print the final count obtained.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Left Rotation and Right Rotation of a String
- Longest subsequence of a number having same left and right rotation
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
- Min flips of continuous characters to make all characters same in a string
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Minimum characters to be replaced to make frequency of all characters same
- Print left rotation of array in O(n) time and O(1) space
- Minimum flips to make all 1s in right and 0s in left
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Check if left and right shift of any string results into given string
- Left rotation of an array using vectors in C++
- Minimum number of flips with rotation to make binary string alternating
- String with k distinct characters and no same characters adjacent
- Reversal algorithm for right rotation of an array
- Minimize steps required to make all array elements same by adding 1, 2 or 5
- Maximize count of corresponding same elements in given Arrays by Rotation
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Minimize length of prefix of string S containing all characters of another string T
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.