Minimum move to end operations to make all strings equal | 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
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 = [ " " , " " , " " , " " ] ; let n = arr . length ; document . write ( minimunMoves ( arr , n ) ) ;