# JavaScript program for Minimum move to end operations to make all strings equal

There are many problems that require a certain level of expertise and creativity to solve. One such problem is determining the minimum number of moves required to make all strings equal. In this article, we will explore how to solve this problem using JavaScript programming. First, let's define the problem.

## Problem Statement

Given an array of strings, we need to find the minimum number of moves required to make all strings equal. In a move, we can move the first character of a string to the end of the same string.

Example

Consider the following array of strings

["abcd", "cdab", "bacd", "cdba"]


We can make all strings equal to "abcd" using the following moves:

• Move the first character of the 2nd string to the end: "cdab" -> "dabc"

• Move the first character of the 3rd string to the end: "bacd" -> "acdb"

• Move the first character of the 4th string to the end: "cdba" -> "dbac"

After these moves, all strings become "abcd". The minimum number of moves required to make all strings equal is 2.

Example 1

Input: n = 4, arr[] = {"abcd", "cdab", "bacd", "cdba"}


Output: Minimum moves: 2

Example 2

Input: n = 2, arr[] = {"molzv", "lzvmo"}


Output: Minimum moves: 2

Example 3

Input: n = 3, arr[] = {"kc", "kc", "kc"}


Output: Minimum moves: 0

Now let’s understand the algorithm for the above problem statement.

## Algorithm

Let's dive into the algorithm to understand the steps involved in finding the minimum number of moves required to make all strings equal

• Initialize a variable "minCnt" to a very large value.

• Iterate through each string in the array.

• For each string, find the minimum number of moves required to make all strings equal to it. To do this, iterate through each string in the array and do the following:

• Append the current string to itself (to create a circular string).

• Find the index of the current string in the circular string. Add this index to a counter "cnt".

• After iterating through all strings in the array, update "minCnt" to the minimum value between "minCnt" and "cnt".

• Return "minCnt" as the minimum number of moves required to make all strings equal.

So let’s move on to implement this algorithm with the help of an example where we implement this algorithm using Javascript.

### Example

The program defines a function called 'minMoves' that takes two arguments, 'str' and 'n'. 'str' is an array of strings and 'n' is the length of the array. The function iterates through each string in the array and, for each string, counts the number of moves required to transform that string into all the other strings in the array. A move is defined as shifting the first character of the string to the end of the string. The function returns the minimum number of moves required to transform all the strings in the array into each other.

The program then defines an array called 'str' and calls the 'minMoves' function with this array and its length. The result is logged to the console as a string that says "Minimum moves: " followed by the minimum number of moves required to transform all the strings in the array into each other.

function minMoves(str, n) {
let minCnt = Infinity;
for (let i = 0; i < n; ++i) {
let cnt = 0;
for (let j = 0; j < n; ++j) {
const temp = str[j] + str[j];
const index = temp.indexOf(str[i]);
if (index !== -1) {
cnt += index;
}
}
minCnt = Math.min(cnt, minCnt);
}
return minCnt;
}
const str = ["abcd", "cdab", "bacd", "cdba"];
console.log("Minimum moves: " + minMoves(str, str.length));


## Conclusion

In this tutorial, we have discussed the problem of finding the minimum number of moves required to make all strings equal. We have presented an algorithm and a corresponding JavaScript program to solve this problem.

The algorithm involves iterating through each string in the array and finding the minimum number of moves required to make all strings equal to it. To do this, we create a circular string by appending the current string to itself and then finding the index of the current string in the circular string. We update a counter with this index value and continue to do this for all strings in the array.

Finally, we return the minimum value of the counter as the minimum number of moves required to make all strings equal. By implementing this algorithm in JavaScript, we can efficiently solve this problem for any given array of strings.

Updated on: 17-Apr-2023

178 Views

Get certified by completing the course