Javascript Program to Minimize characters to be changed to make the left and right rotation of a string same Last Updated : 05 Jul, 2022 Comments Improve Suggest changes Like Article Like 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: 2Explanation: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: 1Explanation: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! Comment More infoAdvertise with us Next Article Javascript Program to Minimize characters to be changed to make the left and right rotation of a string same kartik Follow Improve Article Tags : Strings Greedy Searching Hash JavaScript Web Technologies DSA rotation frequency-counting +5 More Practice Tags : GreedyHashSearchingStrings Similar Reads Minimize characters to be changed to make the left and right rotation of a string same 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: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc 8 min read Javascript Program for Longest subsequence of a number having same left and right rotation Given a numeric string S, the task is to find the maximum length of a subsequence having its left rotation equal to its right rotation. Examples: Input: S = "100210601" Output: 4 Explanation: The subsequence "0000" satisfies the necessary condition. The subsequence "1010" generates the string "0101" 4 min read Javascript Program for Left Rotation and Right Rotation of a String Given a string of size n, write functions to perform the following operations on a string-Left (Or anticlockwise) rotate the given string by d elements (where d <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Le 3 min read Minimize replacement of characters to its nearest alphabet to make a string palindromic Given a string S of length N consisting of lowercase alphabets, the task is to find the minimum number of operations to convert the given string into a palindrome. In one operation, choose any character and replace it by its next or previous alphabet. Note: The alphabets are cyclic i.e., if z is inc 6 min read Minimize replacements by previous or next alphabet required to make all characters of a string the same Given a string S of length N consisting of lowercase alphabets, the task is to find the minimum number of operations required to make all the characters of the string S the same. In each operation, choose any character and replace it with its next or previous alphabet. Note: The alphabets are consid 6 min read Javascript Program for Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end.Examples: Input : n = 2 arr[] = {"molzv", "lzvmo"}Output : 2Explanation: In first string, we removefirst element("m") from first st 2 min read Javascript Program to Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABAOutput : TrueInput : GEEKS, EKSGEOutput : TrueWe have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (longest 2 min read Javascript Program to Check if a string can be obtained by rotating another string 2 places Given two strings, the task is to find if a string can be obtained by rotating another string in two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwiseAsked in: Amazon Int 2 min read Javascript Program to Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str 4 min read Javascript Program for Queries for rotation and Kth character of the given string in constant time Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab 2 min read Like