
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum number of given operations required to make two strings equal using C++.
Problem statement
Given two strings str1 and str2, both strings contain characters ‘a’ and ‘b’. Both strings are of equal lengths. There is one _ (empty space) in both the strings. The task is to convert the first string into the second string by doing the minimum number of the following operations −
If _ is at a position I then _ can be swapped with a character at position i+1 or i-1
If characters at positions i+1 and i+2 are different then _ can be swapped with a character at position i+1 or i+2
Similarly, if characters at positions i-1 and i-2 are different then _ can be swapped with a character at position i-1 or i-2
If str1 = “aba_a” and str2 = “_baaa” then we require 2 moves to transform str1 to str2 −
1. str1 = “ab_aa” (Swapped str1[2] with str1[3]) 2. str2 = “_baaa” (Swapped str1[0] with str1[2])
Algorithm
1. Apply a simple Breadth First Search over the string and an element of the queue used for BFS will contain the pair str, pos where pos is the position of _ in the string str. 2. Also maintain a map namely ‘vis’ which will store the string as key and the minimum moves to get to the string as value. 3. For every string str from the queue, generate a new string tmp based on the four conditions given and update the vis map as vis[tmp] = vis[str] + 1. 4. Repeat the above steps until the queue is empty or the required string is generated i.e. tmp == B 5. If the required string is generated, then return vis[str] + 1 which is the minimum number of operations required to change A to B.
Example
#include <iostream> #include <string> #include <unordered_map> #include <queue> using namespace std; int transformString(string str, string f){ unordered_map<string, int> vis; int n; n = str.length(); int pos = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == '_') { pos = i; break; } } queue<pair<string, int> > q; q.push({ str, pos }); vis[str] = 0; while (!q.empty()) { string ss = q.front().first; int pp = q.front().second; int dist = vis[ss]; q.pop(); if (pp > 0) { swap(ss[pp], ss[pp - 1]); if (!vis.count(ss)) { if (ss == f) { return dist + 1; break; } vis[ss] = dist + 1; q.push({ ss, pp - 1 }); } swap(ss[pp], ss[pp - 1]); } if (pp < n - 1) { swap(ss[pp], ss[pp + 1]); if (!vis.count(ss)) { if (ss == f) { return dist + 1; break; } vis[ss] = dist + 1; q.push({ ss, pp + 1 }); } swap(ss[pp], ss[pp + 1]); } if (pp > 1 && ss[pp - 1] != ss[pp - 2]) { swap(ss[pp], ss[pp - 2]); if (!vis.count(ss)) { if (ss == f) { return dist + 1; break; } vis[ss] = dist + 1; q.push({ ss, pp - 2 }); } swap(ss[pp], ss[pp - 2]); } if (pp < n - 2 && ss[pp + 1] != ss[pp + 2]) { swap(ss[pp], ss[pp + 2]); if (!vis.count(ss)) { if (ss == f) { return dist + 1; break; } vis[ss] = dist + 1; q.push({ ss, pp + 2 }); } swap(ss[pp], ss[pp + 2]); } } return 0; } int main(){ string str1 = "aba_a"; string str2 = "_baaa"; cout << "Minimum required moves: " << transformString(str1, str2) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output −
Minimum required moves: 2
- Related Articles
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Minimum operations required to make all the array elements equal in C++
- Minimum move to end operations to make all strings equal in C++
- Number of operations required to make all array elements Equal in Python
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character in C++
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum operations to make array equal using Python
- Minimum number of given moves required to make N divisible by 25 using C++.
- Find the number of operations required to make all array elements Equal in C++
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Minimum number of operations required to sum to binary string S using C++.
- Minimum operations to make the MEX of the given set equal to x in C++
- Minimum operations of given type to make all elements of a matrix equal in C++
- Minimum Number of Steps to Make Two Strings Anagram in C++
