Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Equalize strings problem solution

YASH PAL, 31 July 2024
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)))
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes