- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum move to end operations to make all strings equal in C++
Problem statement
Given n strings that are permutations of each other. We need to make all strings same with an operation that moves first character of any string to the end of it.
Example
If arr[] = {“abcd”, “cdab”} then 2 moves are required.
- Let us take first string “abcd”. Move character ‘a’ to the end of the string. After this operation string becomes “bcda”
- Now move character ‘b’ to the end of the string. After this operation string becomes “cdab”. Which in turn makes both strings equal
Algorithm
- Take first string. Let us call it as ‘str1’.
Create a temp string by concating str1 to str1 as follows −
temp = str1 + str1
- Count rotations required to make all other strings same as current target
- Repeat above steps for all string and return the minimum of all counts.
Example
#include <iostream> #include <string> #include <algorithm> #include <climits> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int minMoves(string str[], int n) { int minCnt = INT_MAX; for (int i = 0; i < n; ++i) { int cnt = 0; for (int j = 0; j < n; ++j) { string temp = str[j] + str[j]; int index = temp.find(str[i]); if (index != string::npos) { cnt += index; } } minCnt = min(cnt, minCnt); } return minCnt; } int main() { string str[] = {"abcd", "cdab", "bacd", "cdba"}; cout << "Minimum moves: " << minMoves(str, SIZE(str)) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Minimum moves: 2
- Related Articles
- Minimum operations required to make all the array elements equal in C++
- Minimum number of given operations required to make two strings equal using C++.
- Minimum Swaps to Make Strings Equal in C++
- Minimum operations of given type to make all elements of a matrix equal in C++
- Minimum delete operations to make all elements of array same in C++.
- Program to find minimum operations to make array equal using Python
- Minimum operation to make all elements equal in array in C++
- Minimum operations to make the MEX of the given set equal to x in C++
- Minimum number of moves to make all elements equal using C++.
- Program to find minimum operations needed to make two arrays sum equal in Python
- Move all zeroes to end of array in C++
- Find the number of operations required to make all array elements Equal in C++
- Program to find minimum number of operations to move all balls to each box in Python
- Minimum number of operations on an array to make all elements 0 using C++.
- Number of operations required to make all array elements Equal in Python

Advertisements