Minimum number of flips or swaps of adjacent characters required to make two strings equal
Last Updated :
26 Oct, 2023
Given two binary strings A and B of length N, the task is to count the minimum number of operations required to make the two given strings equal by either swapping adjacent characters or flipping any character of the string A.
Examples:
Input: A = “100”, B = “001”
Output: 2
Explanation: Flipping characters A[0](= ‘1’) and A[2](= ‘0’) modifies the string A to “001”, which is equal to the string B.
Input: A = “0101”, B = “0011”
Output: 1
Explanation: Swapping the characters A[2](= ‘0’) and A[3](= ‘1’) modifies the string A to “0011”, which is equal to string B.
Approach: The problem can be solved using Dynamic Programming as it has Overlapping Subproblems and Optimal Substructure.
Follow the steps below to solve the problem:
- Initialize an array, say dp[] of size N+1 as {0}, where dp[i] stores the minimum number of operations required up to index i, to make the prefix of Ai equal to the prefix Bi.
- Iterate over the range [1, N] using a variable, say i, and performing the following operations:
- If A[i – 1] equals B[i – 1], then update dp[i] to dp[i – 1].
- Otherwise, update dp[i] to dp[i – 1] + 1.
- If swapping is possible, i.e. i > 1 and A[i – 2] is equal to B[i – 1] and A[i – 1] is equal to B[i – 2], then update dp[i] to min(dp[i], dp[i – 2] + 1).
- After completing the above steps, print the minimum number of operations obtained, i.e. the value dp[N].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countMinSteps(string A, string B, int N)
{
vector< int > dp(N + 1, 0);
for ( int i = 1; i <= N; i++) {
if (A[i - 1] == B[i - 1]) {
dp[i] = dp[i - 1];
}
else {
dp[i] = dp[i - 1] + 1;
}
if (i >= 2 && A[i - 2] == B[i - 1]
&& A[i - 1] == B[i - 2]) {
dp[i] = min(dp[i], dp[i - 2] + 1);
}
}
return dp[N];
}
int main()
{
string A = "0101" ;
string B = "0011" ;
int N = A.length();
cout << countMinSteps(A, B, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int countMinSteps(String A, String B, int N)
{
int [] dp = new int [N + 1 ];
for ( int i = 1 ; i <= N; i++)
{
dp[i] = 0 ;
}
for ( int i = 1 ; i <= N; i++)
{
if (A.charAt(i - 1 ) == B.charAt(i - 1 ))
{
dp[i] = dp[i - 1 ];
}
else
{
dp[i] = dp[i - 1 ] + 1 ;
}
if (i >= 2 && A.charAt(i - 2 ) == B.charAt(i - 1 ) &&
A.charAt(i - 1 ) == B.charAt(i - 2 ))
{
dp[i] = Math.min(dp[i], dp[i - 2 ] + 1 );
}
}
return dp[N];
}
public static void main(String[] args)
{
String A = "0101" ;
String B = "0011" ;
int N = A.length();
System.out.println(countMinSteps(A, B, N));
}
}
|
Python3
def countMinSteps(A, B, N) :
dp = [ 0 ] * (N + 1 )
for i in range ( 1 , N + 1 ) :
if (A[i - 1 ] = = B[i - 1 ]) :
dp[i] = dp[i - 1 ]
else :
dp[i] = dp[i - 1 ] + 1
if (i > = 2 and A[i - 2 ] = = B[i - 1 ]
and A[i - 1 ] = = B[i - 2 ]) :
dp[i] = min (dp[i], dp[i - 2 ] + 1 )
return dp[N]
A = "0101"
B = "0011"
N = len (A)
print (countMinSteps(A, B, N))
|
C#
using System;
class GFG{
static int countMinSteps( string A, string B, int N)
{
int [] dp = new int [N + 1];
for ( int i = 1; i <= N; i++)
{
dp[i] = 0;
}
for ( int i = 1; i <= N; i++)
{
if (A[i - 1] == B[i - 1])
{
dp[i] = dp[i - 1];
}
else
{
dp[i] = dp[i - 1] + 1;
}
if (i >= 2 && A[i - 2] == B[i - 1] &&
A[i - 1] == B[i - 2])
{
dp[i] = Math.Min(dp[i], dp[i - 2] + 1);
}
}
return dp[N];
}
public static void Main(String []args)
{
string A = "0101" ;
string B = "0011" ;
int N = A.Length;
Console.Write(countMinSteps(A, B, N));
}
}
|
Javascript
<script>
function countMinSteps(A, B, N) {
let dp = new Array(N + 1).fill(0);
for (let i = 1; i <= N; i++) {
if (A[i - 1] == B[i - 1]) {
dp[i] = dp[i - 1];
}
else {
dp[i] = dp[i - 1] + 1;
}
if (i >= 2 && A[i - 2] == B[i - 1]
&& A[i - 1] == B[i - 2]) {
dp[i] = Math.min(dp[i], dp[i - 2] + 1);
}
}
return dp[N];
}
let A = "0101" ;
let B = "0011" ;
let N = A.length;
document.write(countMinSteps(A, B, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient approach : Space optimization O(1)
In previous approach we the current value dp[i] is only depend upon the previous 2 values i.e. dp[i-1] and dp[i-2]. So to optimize the space we can keep track of previous and current values by the help of three variables prev1, prev2 and curr which will reduce the space complexity from O(N) to O(1).
Implementation Steps:
- Create 2 variables prev1 and prev2 to keep track o previous values of DP.
- Initialize base case prev1 = prev2 = 0.
- Create a variable curr to store current value.
- Iterate over subproblem using loop and update curr.
- After every iteration update prev1 and prev2 for further iterations.
- At last return curr.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countMinSteps(string A, string B, int N)
{
int prev1 = 0, prev2 = 0, curr = 0;
for ( int i = 1; i <= N; i++) {
if (A[i - 1] == B[i - 1]){
curr = prev1;
}
else {
curr = prev1 + 1;
}
if (i >= 2 && A[i - 2] == B[i - 1] && A[i - 1] == B[i - 2]){
curr = min(curr, prev2 + 1);
}
prev2 = prev1;
prev1 = curr;
}
return curr;
}
int main()
{
string A = "0101" ;
string B = "0011" ;
int N = A.length();
cout << countMinSteps(A, B, N);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static int countMinSteps(String A, String B, int N)
{
int prev1 = 0 , prev2 = 0 , curr = 0 ;
for ( int i = 1 ; i <= N; i++) {
if (A.charAt(i - 1 ) == B.charAt(i - 1 )) {
curr = prev1;
} else {
curr = prev1 + 1 ;
}
if (i >= 2 && A.charAt(i - 2 ) == B.charAt(i - 1 ) && A.charAt(i - 1 ) == B.charAt(i - 2 )) {
curr = Math.min(curr, prev2 + 1 );
}
prev2 = prev1;
prev1 = curr;
}
return curr;
}
public static void main(String[] args) {
String A = "0101" ;
String B = "0011" ;
int N = A.length();
System.out.println(countMinSteps(A, B, N));
}
}
|
Python
def countMinSteps(A, B, N):
prev1, prev2, curr = 0 , 0 , 0
for i in range ( 1 , N + 1 ):
if A[i - 1 ] = = B[i - 1 ]:
curr = prev1
else :
curr = prev1 + 1
if i > = 2 and A[i - 2 ] = = B[i - 1 ] and A[i - 1 ] = = B[i - 2 ]:
curr = min (curr, prev2 + 1 )
prev2, prev1 = prev1, curr
return curr
A = "0101"
B = "0011"
N = len (A)
print (countMinSteps(A, B, N))
|
C#
using System;
public class GFG {
public static int CountMinSteps( string A, string B,
int N)
{
int prev1 = 0, prev2 = 0, curr = 0;
for ( int i = 1; i <= N; i++) {
if (A[i - 1] == B[i - 1]) {
curr = prev1;
}
else {
curr = prev1 + 1;
}
if (i >= 2 && A[i - 2] == B[i - 1]
&& A[i - 1] == B[i - 2]) {
curr = Math.Min(curr, prev2 + 1);
}
prev2 = prev1;
prev1 = curr;
}
return curr;
}
public static void Main( string [] args)
{
string A = "0101" ;
string B = "0011" ;
int N = A.Length;
Console.WriteLine(CountMinSteps(A, B, N));
}
}
|
Javascript
function countMinSteps(A, B, N) {
let prev1 = 0;
let prev2 = 0;
let curr = 0;
for (let i = 1; i <= N; i++) {
if (A[i - 1] === B[i - 1]) {
curr = prev1;
} else {
curr = prev1 + 1;
}
if (i >= 2 && A[i - 2] === B[i - 1] && A[i - 1] === B[i - 2]) {
curr = Math.min(curr, prev2 + 1);
}
prev2 = prev1;
prev1 = curr;
}
return curr;
}
const A = "0101" ;
const B = "0011" ;
const N = A.length;
document.write(countMinSteps(A, B, N));
|
Output:
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...