Saturday, October 25, 2025
HomeLanguagesJavascriptJavascript Program for Minimum move to end operations to make all strings...

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 : 2
Explanation: In first string, we remove
first element("m") from first string and 
append it end. Then we move second character
of first string and move it to end. So after
2 operations, both strings become same.

Input : n = 3
        arr[] = {"kc", "kc", "kc"}
Output : 0
Explanation: 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




<script>
 
// 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;
    document.write(minimunMoves(arr, n));
    
     
    // This code is contributed by avanitrachhadiya2155
     
</script>


Output:  

5

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!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS