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" ? "abcd" (2 moves)

  • Move the first character of the 3rd string to the end: "bacd" ? "acdb" ? "cdba" ? "dbac" ? "bacd" (impossible to reach "abcd")

  • Move the first character of the 4th string to the end: "cdba" ? "dbac" ? "bacd" ? "acdb" ? "cdba" (impossible to reach "abcd")

The minimum number of moves required to make all strings equal is 2.

Examples

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

Algorithm

The algorithm works by trying each string as a potential target and calculating the minimum moves needed:

  • Initialize a variable "minCnt" to infinity.

  • For each string in the array, treat it as the target string.

  • For each target, calculate the total moves needed to transform all other strings:

  • Create a circular version by appending the string to itself.

  • Find the position of the target string in this circular version.

  • The position represents the number of moves needed.

  • Keep track of the minimum total moves across all possible targets.

  • Return the minimum number of moves.

Implementation

function minMoves(str, n) {
    let minCnt = Infinity;
    
    // Try each string as a potential target
    for (let i = 0; i < n; ++i) {
        let cnt = 0;
        
        // Calculate moves needed for all strings to match str[i]
        for (let j = 0; j < n; ++j) {
            // Create circular string by appending to itself
            const temp = str[j] + str[j];
            
            // Find how many moves needed to transform str[j] to str[i]
            const index = temp.indexOf(str[i]);
            
            if (index !== -1) {
                cnt += index;
            } else {
                // If transformation is impossible, skip this target
                cnt = Infinity;
                break;
            }
        }
        
        // Update minimum count
        minCnt = Math.min(cnt, minCnt);
    }
    
    return minCnt === Infinity ? -1 : minCnt;
}

// Test cases
const str1 = ["abcd", "cdab", "bacd", "cdba"];
console.log("Test 1 - Minimum moves: " + minMoves(str1, str1.length));

const str2 = ["molzv", "lzvmo"];
console.log("Test 2 - Minimum moves: " + minMoves(str2, str2.length));

const str3 = ["kc", "kc", "kc"];
console.log("Test 3 - Minimum moves: " + minMoves(str3, str3.length));
Test 1 - Minimum moves: 2
Test 2 - Minimum moves: 2
Test 3 - Minimum moves: 0

How It Works

The algorithm creates a circular representation of each string by concatenating it with itself. For example, "cdab" becomes "cdabcdab". When we search for "abcd" in this circular string, we find it at index 2, meaning we need 2 moves to transform "cdab" into "abcd".

The key insight is that moving the first character to the end is equivalent to rotating the string left by one position. By trying each string as a target, we find the optimal solution that minimizes total moves.

Time Complexity

The time complexity is O(n² × m), where n is the number of strings and m is the average length of strings. The space complexity is O(m) for creating the temporary circular strings.

Conclusion

This algorithm efficiently solves the minimum moves problem by testing each string as a potential target and calculating the rotation distance using circular string concatenation. The approach guarantees finding the optimal solution for making all strings equal through left rotations.

Updated on: 2026-03-15T23:19:01+05:30

389 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements