Minimum move to end operations to make all strings equal | CPP program to make all strings same using move to end operations . ; Returns minimum number of moves to end operations to make all strings same . ; Consider s [ i ] as target string and count rotations required to make all other strings same as str [ i ] . ; find function returns the index where we found arr [ i ] which is actually count of move - to - front operations . ; If any two strings are not rotations of each other , we can 't make them same.  ; driver code for above function .
#include <bits/stdc++.h> NEW_LINE 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 ; }