Minimum move to end operations to make all strings equal
Last Updated :
18 Sep, 2023
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 a number of move to front operations required to make two strings the same. We one by one consider every string as the target string. We count rotations required to make all other strings the same as the current target and finally return a minimum of all counts.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimunMoves(string arr[], int n)
{
int ans = INT_MAX;
for ( int i = 0; i < n; i++)
{
int curr_count = 0;
for ( int j = 0; j < n; j++) {
string tmp = arr[j] + arr[j];
int index = tmp.find(arr[i]);
if (index == string::npos)
return -1;
curr_count += index;
}
ans = min(curr_count, ans);
}
return ans;
}
int main()
{
string arr[] = { "xzzwo" , "zwoxz" , "zzwox" , "xzzwo" };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << minimunMoves(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minimunMoves(String arr[], int n)
{
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
{
int curr_count = 0 ;
String tmp = "" ;
for ( int j = 0 ; j < n; j++)
{
tmp = arr[j] + arr[j];
int index = tmp.indexOf(arr[i]);
if (index != - 1 )
curr_count += index;
else
curr_count = - 1 ;
}
ans = Math.min(curr_count, ans);
}
return ans;
}
public static void main(String args[])
{
String arr[] = { "xzzwo" , "zwoxz" ,
"zzwox" , "xzzwo" };
int n = arr.length;
System.out.println(minimunMoves(arr, n));
}
}
|
Python 3
import sys
def minimunMoves(arr, n):
ans = sys.maxsize
for i in range (n):
curr_count = 0
for j in range (n):
tmp = arr[j] + arr[j]
index = tmp.find(arr[i])
if (index = = len (arr[i])):
return - 1
curr_count + = index
ans = min (curr_count, ans)
return ans
if __name__ = = "__main__" :
arr = [ "xzzwo" , "zwoxz" , "zzwox" , "xzzwo" ]
n = len (arr)
print ( minimunMoves(arr, n))
|
C#
using System;
public class GFG
{
public static int minimunMoves( string [] arr, int n)
{
int ans = int .MaxValue;
for ( int i = 0; i < n; i++)
{
int curr_count = 0;
string tmp = "" ;
for ( int j = 0; j < n; j++)
{
tmp = arr[j] + arr[j];
int index = tmp.IndexOf(arr[i], StringComparison.Ordinal);
if (index == arr[i].Length)
{
return -1;
}
curr_count += index;
}
ans = Math.Min(curr_count, ans);
}
return ans;
}
public static void Main( string [] args)
{
string [] arr = new string [] { "xzzwo" , "zwoxz" , "zzwox" , "xzzwo" };
int n = arr.Length;
Console.WriteLine(minimunMoves(arr, n));
}
}
|
Javascript
<script>
function minimunMoves(arr,n)
{
let ans = Number.MAX_VALUE;
for (let i = 0; i < n; i++)
{
let curr_count = 0;
let tmp = "" ;
for (let j = 0; j < n; j++)
{
tmp = arr[j] + arr[j];
let index = tmp.indexOf(arr[i]);
if (index == arr[i].length)
return -1;
curr_count += index;
}
ans = Math.min(curr_count, ans);
}
return ans;
}
let arr=[ "xzzwo" , "zwoxz" ,
"zzwox" , "xzzwo" ];
let n = arr.length;
document.write(minimunMoves(arr, n));
</script>
|
Time Complexity : O(n3), Where n is the size of given string (n2 for the two nested for loops and n is for the function used as find())
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...