Given two binary strings A and B of length N, the task is to convert the string A to B by either flipping any character of A or swapping adjacent characters of A minimum number of times. If it is not possible to make both the strings equal, print -1.
Examples:
Input: A = “10010010”, B = “00001000”
Output: 3
Explanation:
Operation 1: Flipping A[0] modifies A to “00010010”.
Operation 2: Flipping A[6] modifies A to “00010000”.
Operation 3: Swapping A[3] and A[4] modifies A to “00001000”
Therefore, the total number of operations is 3.
Input: A = “11”, B = “00”
Output: 3
Approach: The idea is to traverse the string A and try to make the same-indexed characters equal by first checking for the condition of swapping the adjacent characters. If the characters can not be made equal by this operation, then flip the character. Follow the steps below to solve the problem:
- Initialize a variable, say ans, to store the required result.
- Traverse the string A using a variable, say i, and perform the following operations:
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperation(string a, string b)
{
int n = a.length();
int i = 0;
int minoperation = 0;
while (i < n) {
if (a[i] == b[i]) {
i = i + 1;
continue ;
}
else if (a[i] == b[i + 1]
&& a[i + 1] == b[i]
&& i < n - 1) {
minoperation++;
i = i + 2;
}
else if (a[i] != b[i]) {
minoperation++;
i = i + 1;
}
else {
++i;
}
}
cout << minoperation;
}
int main()
{
string a = "10010010" , b = "00001000" ;
minimumOperation(a, b);
return 0;
}
|
Java
public class GFG
{
static void minimumOperation(String a, String b)
{
int n = a.length();
int i = 0 ;
int minoperation = 0 ;
while (i < n)
{
if (a.charAt(i) == b.charAt(i))
{
i = i + 1 ;
continue ;
}
else if (a.charAt(i) == b.charAt(i + 1 ) &&
a.charAt(i + 1 ) == b.charAt(i) &&
i < n - 1 )
{
minoperation++;
i = i + 2 ;
}
else if (a.charAt(i) != b.charAt(i))
{
minoperation++;
i = i + 1 ;
}
else
{
++i;
}
}
System.out.println(minoperation);
}
public static void main(String []args)
{
String a = "10010010" , b = "00001000" ;
minimumOperation(a, b);
}
}
|
Python3
def minimumOperation(a, b):
n = len (a)
i = 0
minoperation = 0
while (i < n):
if (a[i] = = b[i]):
i = i + 1
continue
elif (a[i] = = b[i + 1 ] and
a[i + 1 ] = = b[i] and i < n - 1 ):
minoperation + = 1
i = i + 2
elif (a[i] ! = b[i]):
minoperation + = 1
i = i + 1
else :
i + = 1
print (minoperation)
if __name__ = = '__main__' :
a = "10010010"
b = "00001000"
minimumOperation(a, b)
|
C#
using System;
class GFG{
static void minimumOperation( string a, string b)
{
int n = a.Length;
int i = 0;
int minoperation = 0;
while (i < n)
{
if (a[i] == b[i])
{
i = i + 1;
continue ;
}
else if (a[i] == b[i + 1] &&
a[i + 1] == b[i] &&
i < n - 1)
{
minoperation++;
i = i + 2;
}
else if (a[i] != b[i])
{
minoperation++;
i = i + 1;
}
else
{
++i;
}
}
Console.WriteLine(minoperation);
}
public static void Main()
{
string a = "10010010" , b = "00001000" ;
minimumOperation(a, b);
}
}
|
Javascript
<script>
function minimumOperation(a, b)
{
var n = a.length;
var i = 0;
var minoperation = 0;
while (i < n) {
if (a[i] == b[i]) {
i = i + 1;
continue ;
}
else if (a[i] == b[i + 1]
&& a[i + 1] == b[i]
&& i < n - 1) {
minoperation++;
i = i + 2;
}
else if (a[i] != b[i]) {
minoperation++;
i = i + 1;
}
else {
++i;
}
}
document.write(minoperation);
}
var a = "10010010" , b = "00001000" ;
minimumOperation(a, b);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)