Minimize characters to be changed to make the left and right rotation of a string same


When working with strings, it's common to encounter problems that involve rotation, a process that reorders the characters in a string by moving a certain number of characters to the opposite end of the string. In this article, we will explore an interesting problem: how to minimize the number of characters that must be changed to make the left and right rotation of a string the same. We will provide a well-structured C++ solution and include an example to illustrate the test case.

Problem Statement

Given a string 's' of length 'n', we need to find the minimum number of characters to be changed in the string so that after performing a left rotation and a right rotation of the same number of positions, the two resultant strings are the same.

Algorithm

  • Create a function minCharsToChange that takes the string 's' as an argument.

  • Initialize a variable 'minChanges' to the length of the string.

  • Iterate through the string with a loop, incrementing an index 'i' from 0 to 'n'.

  • In each iteration, perform the following steps:

    • Perform a left rotation of 'i' positions and store the result in a new string 'leftRotated'.

    • Perform a right rotation of 'i' positions and store the result in a new string 'rightRotated'.

    • Calculate the number of differing characters between 'leftRotated' and 'rightRotated', storing the result in a variable 'diffCount'.

    • Update 'minChanges' to the minimum value between 'minChanges' and 'diffCount'.

  • Return 'minChanges' as the result.

C++ Implementation

Example

#include <iostream>
#include <string>
#include <algorithm>

// Function to perform left rotation
std::string leftRotate(std::string s, int d) {
   std::rotate(s.begin(), s.begin() + d, s.end());
   return s;
}

// Function to perform right rotation
std::string rightRotate(std::string s, int d) {
   std::rotate(s.begin(), s.begin() + s.size() - d, s.end());
   return s;
}

// Function to minimize characters to be changed
int minCharsToChange(std::string s) {
   int n = s.length();
   int minChanges = n;
   
   for (int i = 0; i < n; ++i) {
      std::string leftRotated = leftRotate(s, i);
      std::string rightRotated = rightRotate(s, i);
      int diffCount = 0;
   
      for (int j = 0; j < n; ++j) {
         if (leftRotated[j] != rightRotated[j]) {
               ++diffCount;
         }
      }
   
      minChanges = std::min(minChanges, diffCount);
   }

   return minChanges;
}

int main() {
   std::string s = "abca";
   int result = minCharsToChange(s);
   std::cout << "Minimum characters to be changed: " << result << std::endl;
   
   return 0;
}

Output

Minimum characters to be changed: 0

Testcase Example

Consider the following string: "abca". The possible left and right rotations with their respective differing characters are as follows −

  • i = 0: Left rotation = "abca", Right rotation = "abca", Differing characters = 0

  • i = 1: Left rotation = "bcaa", Right rotation = "caab", Differing characters = 3

  • i = 2: Left rotation = "caab", Right rotation = "bcac", Differing characters = 2

  • i = 3: Left rotation = "bcac", Right rotation = "bcaa", Differing characters = 3

From the iterations above, we can see that the minimum number of differing characters is 0, which occurs when i = 0. In this case, no changes are required to make the left and right rotation of the string the same.

Conclusion

In this article, we explored the problem of minimizing the number of characters to be changed to make the left and right rotation of a string the same. We presented a clear and efficient C++ implementation that utilizes a simple loop to iterate through the string, performing left and right rotations and comparing the resulting strings to find the minimum number of changes required.

By understanding this algorithm, you can apply similar concepts to solve other string manipulation and rotation problems in computer science and programming.

Updated on: 18-May-2023

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements