Open In App

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

Last Updated : 05 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Examples:

Input: S = “abcd”
Output: 2
Explanation:
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”
Output: 1
Explanation:
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:

Javascript




<script>
      // JavaScript program of the
      // above approach
      // Function to find the minimum
      // characters to be removed from
      // the string
      function getMinimumRemoval(str) {
        var n = str.length;
 
        // Initialize answer by N
        var ans = n;
 
        // If length is even
        if (n % 2 === 0) {
          // Frequency array for odd
          // and even indices
          var freqEven = new Array(128).fill(0);
          var freqOdd = new Array(128).fill(0);
 
          // Store the frequency of the
          // characters at even and odd
          // indices
          for (var i = 0; i < n; i++) {
            if (i % 2 === 0) {
              freqEven[str[i].charCodeAt(0)]++;
            } else {
              freqOdd[str[i].charCodeAt(0)]++;
            }
          }
 
          // Stores the most occurring frequency
          // for even and odd indices
          var evenMax = 0,
            oddMax = 0;
 
          for (var chr = "a".charCodeAt(0); chr <= "z".charCodeAt(0); chr++) {
            evenMax = Math.max(evenMax, freqEven[chr]);
            oddMax = Math.max(oddMax, freqOdd[chr]);
          }
 
          // Update the answer
          ans = ans - evenMax - oddMax;
        }
 
        // If length is odd
        else {
          // Stores the frequency of the
          // characters of the string
          var freq = new Array(128).fill(0);
          for (var i = 0; i < n; i++) {
            freq[str[i].charCodeAt(0)]++;
          }
 
          // Stores the most occurring character
          // in the string
          var strMax = 0;
          for (var chr = "a".charCodeAt(0); chr <= "z".charCodeAt(0); chr++) {
            strMax = Math.max(strMax, freq[chr]);
          }
 
          // Update the answer
          ans = ans - strMax;
        }
        return ans;
      }
 
      // Driver code
      var str = "geeksgeeks";
      document.write(getMinimumRemoval(str));
    </script>


Output: 

6

Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time 
Auxiliary Space: O(1), as we are not using any extra space.

Please refer complete article on Minimize characters to be changed to make the left and right rotation of a string same for more details!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads