Python3 Program to Minimize Characters to be Changed to Make the Left and Right Rotation of a String the Same


Rotation means we have to shift each character either in a forward direction or backward direction. Forward direction means right rotation (Or anticlockwise) and backward direction means left rotation (Or clockwise).

In this problem, we have given a string of size n. Our task is to find the minimum number of the character to be changed to check whether it is possible to the make left rotation and right rotation of a string the same.

Let's see examples with explanations below to understand the problem in a better way.

Input 1

str = "wxyz"

Output 1

2

Explanation

The left rotation of the string is xyzw

The right rotation of the string is zwxy

So as per the observation change the character at the position of 0 str[0] = w to y and at the position of 3 str[3] = z to x. The string becomes yxyx.

Therefore, the answer is 2 as the both left and right rotation string become xyxy.

Input 2

str = "aka"

Output 2

1

Explanation

The left rotation of the string is aak

The right rotation of the string is kaa

So as per the observation change the character at the position of 1 str[1] = k to a. The string becomes aaa.

Therefore, the answer is 1 as the both left and right rotation string become aaa.

Approach

After seeing the example for the given string above, let's move on to the method.

The idea of this approach is based on observation. The observation has two points-

  • when we have a string of even length then all the characters present in the string in the even index and odd index must be the same to get both right and left rotated strings the same.

  • When we have a string of odd length then all the characters present in the string must be same to get both right and left-rotated strings the same.

Let's see the code below for a better understanding of the above approach.

Example

# Function to find the minimum characters to be removed from the string
def getMinimumChange(str, n):
   # Initialize answer by N in variable minNum
   minChanges = n
   # If the length of the string is even
   if (n % 2 == 0):
      # Created frequency array for Even index
      freqEvenIdx = {}
      # Created frequency array for odd index
      freqOddIdx = {}
      # Traverse for loop to intialize both the frequency array freqEvenddIdx and freqOddIdx to 0
      for ch in range(ord('a'), ord('z') + 1):
         freqEvenIdx[chr(ch)] = 0
         freqOddIdx[chr(ch)] = 0
      # at odd and even index
      for i in range(n):
         if (i % 2 == 0):
            if str[i] in freqEvenIdx:
               freqEvenIdx[str[i]] += 1
         else:
            if str[i] in freqOddIdx:
               freqOddIdx[str[i]] += 1
      # Create variable evenIdxMax and OddIdxMax to store maximum frequency of even place character and odd place character respectively
      evenIdxMax = 0
      oddIdxMax = 0
      # Traverse for loop from a to z to update variable evenIdxMax and OddIdxMax
      for ch in range(ord('a'), ord('z') + 1):
         evenIdxMax = max(evenIdxMax, freqEvenIdx[chr(ch)])
         oddIdxMax = max(oddIdxMax, freqOddIdx[chr(ch)])
      # Update the answerin variable minChanges
      minChanges = minChanges - evenIdxMax - oddIdxMax
   # If the length of the string is odd
   else:
      # Create array to store frequecy of character of the string
      freq = {}
      #  initialize the the freq array
      for ch in range('a', 'z'):
         freq[chr(ch)] = 0
      # Stores the frequency of the characters of the string while traversing the string
      for i in range(n):
         if str[i] in freq:
            freq[str[i]] += 1
      # Stores the maximum freuency of character of the string in variable freqMax
      freqMax = 0
      #  Traverse the for loop to update freqMax
      for ch in range('a', 'z'):
         freqMax = max(freqMax, freq[chr(ch)])
      # Update the answer in variable minChanges
      minChanges = minChanges - freqMax
# Return final answer minChanges
   return minChanges
str = "wxyz"  # Given String
n = len(str)  # Getting size of the given string
# Call the function to get minimum number of changes to make left and right rotated string same
res = getMinimumChange(str, n)
# Print result
print(
   "The minimum number of changes to make the left and right rotated string same are: "
)
print(res)

Output

The minimum number of changes to make the left and right rotated string same are: 
2

Time and Space Complexity

The time complexity of the above code is O(N), as we traverse both the spring and number.

The space complexity of the above code is O(1), as no extra space is used to store anything.

Where N is the size of the string.

Conclusion

In this tutorial, we have implemented a Python3 Program to Minimize characters to be changed to make the left and right rotation of a string the same. We have implemented an approach of hashing as we have to store the frequency. The time complexity is O(N) and the space complexity of O(1) and N is the size of the given string.

Updated on: 11-Jul-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements