C++ Program for Minimum move to end operations to make all strings equal Last Updated : 19 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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. C++ // CPP program to make all strings same using // move to end operations. #include <bits/stdc++.h> using namespace std; // Returns minimum number of moves to end // operations to make all strings same. int minimunMoves(string arr[], int n) { int ans = INT_MAX; for (int i = 0; i < n; i++) { int curr_count = 0; // Consider s[i] as target string and // count rotations required to make // all other strings same as str[i]. for (int j = 0; j < n; j++) { string tmp = arr[j] + arr[j]; // find function returns the index where we // found arr[i] which is actually count of // move-to-front operations. int index = tmp.find(arr[i]); // If any two strings are not rotations of // each other, we can't make them same. if (index == string::npos) return -1; curr_count += index; } ans = min(curr_count, ans); } return ans; } // driver code for above function. int main() { string arr[] = {"xzzwo", "zwoxz", "zzwox", "xzzwo"}; int n = sizeof(arr)/sizeof(arr[0]); cout << minimunMoves(arr, n); return 0; } Output: 5 Time Complexity: O(N3) (N2 due to two nested loops used and N is for the function find() used the inner for loop) Space Complexity: O(1) since constant variables are used Please refer complete article on Minimum move to end operations to make all strings equal for more details! Comment More infoAdvertise with us Next Article C++ Program for Minimum move to end operations to make all strings equal kartik Follow Improve Article Tags : Strings C++ Programs C++ DSA rotation +1 More Practice Tags : CPPStrings Similar Reads Minimum operations required to convert all characters of a String to a given Character Given string str, a character ch, and an integer K, the task is to find the minimum number of operations required to convert all the characters of string str to ch. Each operation involves converting K closest characters from each side of the index i, i.e., characters in the indices [i - K, i + K] c 9 min read C++ Program for Minimum rotations required to get the same string Given a string, we need to find the minimum number of rotations required to get the same string. Examples: Input : s = "geeks" Output : 5 Input : s = "aaaa" Output : 1 The idea is based on below post.A Program to check if strings are rotations of each other or not Step 1: Initialize result = 0 (Here 2 min read Reduce the string to minimum length with the given operation Given a string str consisting of lowercase and uppercase characters, the task is to find the minimum possible length the string can be reduced to after performing the given operation any number of times. In a single operation, any two consecutive characters can be removed if they represent the same 6 min read C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad 1 min read C++ Program to Minimize characters to be changed to make the left and right rotation of a string same Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = âabcdâOutput: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc 3 min read C++ Program to Modify a string by performing given shift operations Given a string S containing lowercase English alphabets, and a matrix shift[][] consisting of pairs of the form{direction, amount}, where the direction can be 0 (for left shift) or 1 (for right shift) and the amount is the number of indices by which the string S is required to be shifted. The task i 3 min read How to Compare Two Substrings in a Character Array in C++? In C++, character arrays are used to store a sequence of characters also known as strings. A Substring is a continuous sequence of characters within a string. In this article, we will learn how we can compare two substrings in a character array in C++. Examples: Input: string: "Hello World" Substrin 2 min read String C/C++ Programs C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate 3 min read Java 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 3 min read Python3 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 : 2Explanation: In first string, we removefirst element("m") from first st 2 min read Like