Javascript Program to Minimize Characters to be Changed to make the Left and Right Rotation of a String Same


In this problem, we require to determine the minimal cost to make the string’s left and right rotation same.

Here is the observation which we will use to solve the problem.

  • All characters should be equal for strings with odd lengths to make the left and right rotations the same.

  • The string with an even length should have characters same at the even and odd indexes.

Problem statement – We have a string of size N containing the different characters. We need to determine the minimum cost to make the left and right rotations of the given string same by changing the characters of the given string. The cost to change any character of the string is 1.

Note – The left rotation of the string moves the string in the left direction by 1 position and puts the first character at the end of the string.

The right rotation of the string moves the string in the right direction by 1 position and puts the first character at the start of the string.

Sample examples

Input

str = ‘tut’

Output

‘1’

Explanation – As the string length is odd, we need to make all characters same. So, we need to change ‘u’ to make it equal ‘t’.

Input

‘gggggggggggg’

Output

0

Explanation – As all characters of the string are same, we don’t need to change any character.

Input

str = "mpmntrmnfdmn";

Output

5

Explanation – As string length is even, we need to make all characters equal at even index and odd indexes. At the even index, we can make all characters equal to the ‘m’, and at the odd index, we can make all characters equal to the ‘n’.

Approach 1

In this approach, we will make all characters equal in the odd-length string and equal at the even and odd index for the even-length string.

We can find the character with maximum frequency for odd-length strings and make all characters equal to it. Furthermore, we can find the character with maximum frequency at odd and even positions and change the characters at even and odd index accordingly for even-length strings.

Algorithm

Step 1 – Initialize the ‘str_size’ variable with the string length.

Step 2 –Define the ‘minCost’ variable to store the minimum cost of the changing characters to make the left and right rotations equal.

Step 3 – If str_size % 2 equals zero, we need to follow the steps below.

Step 3.1 – Define evenFreq, and oddFreq array variables of size 128 and initialize all array elements with zero.

Step 3.2 – Traverse the string, and use the charCodeAt() method to get the ASCII value of the character. If the current index is even, update the character frequency in the evenFreq array. Otherwise, update the character frequency in the oddFreq array.

Step 3.3 – After getting the frequency of each character at the even indexes and odd indexes, we need to find the character with the maximum frequency. So, declare the maxiEven and maxiOdd variables.

Step 3.4 – Find the maximum values from the array and store them in maxiEven and maxiOdd variables.

Step 3.5 – Subtract the maxiEven and maxiOdd from the string length and assign it to the minCost variable.

Step 4 – For the string with an odd length, we should follow the below steps.

Step 4.1 – Define the charFreq array to store the frequency of all characters of the string.

Step 4.2 – Traverse the string and update the character's frequency In the array.

Step 4.3 – Find the maximum value from the array.

Step 4.4 – Subtract the maxFreq from the string length and assign it to the minCost.

Step 5 – Return minCost value.

Example

function findMinCharacters(str) {
    var str_size = str.length;  
    // Base case if all characters are different in a given string
    var minCost = str_size;  
    // For even size of strings
    if (str_size % 2 === 0) {
      // Defining array to store the frequency
      var evenFreq = new Array(128).fill(0);
      var oddFreq = new Array(128).fill(0);
        // Traverse the string
      for (var p = 0; p < str_size; p++) {
        if (p % 2 === 0) {
          // Update character frequency in the array based on the odd or even index
          evenFreq[str[p].charCodeAt(0)]++;
        } else {
          oddFreq[str[p].charCodeAt(0)]++;
        }
      }
      var maxiEven = 0;
      var maxiOdd = 0;
        for (var c = "a".charCodeAt(0); c <= "z".charCodeAt(0); c++) {
        maxiEven = Math.max(maxiEven, evenFreq[c]);
        maxiOdd = Math.max(maxiOdd, oddFreq[c]);
      }
        // Final answer
      minCost = minCost - maxiEven - maxiOdd;
    }
      // For the odd length of string
    else {
      var charFreq = new Array(128).fill(0);  
      // store character frequency in the array
      for (var p = 0; p < str_size; p++) {
        charFreq[str[p].charCodeAt(0)]++;
      }  
      // Finding the maximum frequency of any character
      var maxFreq = 0;
      for (var c = "a".charCodeAt(0); c <= "z".charCodeAt(0); c++) {
        maxFreq = Math.max(maxFreq, charFreq[c]);
      }
     // Final answer
      minCost = minCost - maxFreq;
    }
  
    //   returning the answer
    return minCost;
  }  
  var str = "tut";
  console.log(
    "The minimum number of characters we should remove to get a left and right rotation of string same is - " +
      findMinCharacters(str)
  );

Output

The minimum number of characters we should remove to get a left and right rotation of string same is - 1

Time complexity – O(N) as we traverse the string of length N.

Space complexity – O(N) as we use the constant space.

From this problem solution, we can learn that every problem doesn’t have a specific approach to solve the problem. Sometimes, we require to observe the problem inputs and outputs and solve the problem based on that.

Updated on: 25-Aug-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements