In this HackerEarth Equalize strings problem solution, You are given two binary strings A and B and are required to make A equals to B. There are two types of operations:
  1. Swap any two adjacent bits or characters in string A. The cost of this operation is 1 unit.
  2. Flip the bit or character of the string. The cost of this operation is 1 unit.
What is the minimum cost to make string A equal to B?


HackerEarth Equalize strings problem solution


HackerEarth Equalize strings problem solution.

#include <iostream>
using namespace std;
long long i,n,s=0;
string a,b;
int main(){
cin>>n>>a>>b;
for(i=0;i<n;i++)
if(a[i]!=b[i]){
if(i<n-1&&a[i]==b[i+1]&&b[i]==a[i+1])
i++;
s++;
}
cout<<s;
return 0;
}

Second solution

n = int(input())
s = list(input())
p = list(input())
ans = 0
for i in range(0, n - 1):
if s[i] == p[i + 1] and s[i + 1] == p[i] and s[i] != s[i + 1]:
s[i], s[i + 1] = s[i + 1], s[i]
ans += 1
print(ans + sum(s[i] != p[i] for i in range(n)))