Javascript Program for Minimum move to end operations to make all strings equal Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 string and append it end. Then we move second characterof first string and move it to end. So after2 operations, both strings become same.Input : n = 3 arr[] = {"kc", "kc", "kc"}Output : 0Explanation: already all strings are equal.The move to end operation is basically left rotation. We use the approach discussed in check if strings are rotations of each other or not to count number of move to front operations required to make two strings same. We one by one consider every string as the target string. We count rotations required to make all other strings same as current target and finally return minimum of all counts.Below is the implementation of above approach. JavaScript // Javascript program to make all // strings same using move // to end operations. // Returns minimum number of // moves to end operations // to make all strings same. function minimunMoves(arr, n) { let ans = Number.MAX_VALUE; for (let i = 0; i < n; i++) { let curr_count = 0; // Consider s[i] as target // string and count rotations // required to make all other // strings same as str[i]. let tmp = ""; for (let j = 0; j < n; j++) { tmp = arr[j] + arr[j]; // find function returns the // index where we found arr[i] // which is actually count of // move-to-front operations. let index = tmp.indexOf(arr[i]); // If any two strings are not // rotations of each other, // we can't make them same. if (index == arr[i].length) return -1; curr_count += index; } ans = Math.min(curr_count, ans); } return ans; } // Driver code let arr = ["xzzwo", "zwoxz", "zzwox", "xzzwo"]; let n = arr.length; console.log(minimunMoves(arr, n)); Output5 Complexity Analysis:Time Complexity: O(N3) (N2 due to two nested loops used and N is for the function indexOf() used the inner for loop)Please refer complete article on Minimum move to end operations to make all strings equal for more details! Create Quiz Comment K kartik Follow 0 Improve K kartik Follow 0 Improve Article Tags : JavaScript rotation Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like