Equalize strings

2.8

9 votes
Greedy Algorithms, Algorithms, String, Basics of Greedy Algorithms
Problem

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?

Input format

  • The first line contains one integer that denotes the length of both strings.
  • The next two lines take two strings A and B of length n as an input.

Output format

Print one integer that represents the minimum cost required to convert string A to string B.

Constraints

1n105

Note

The length of both the strings A and B is always the same.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

We will flip the first character which will cost us 1 unit. Now the string A will be "0101". We will swap second and third characters which will also cost us 1 unit.Now the string A will be "0011" which is same as B.Hence our total cost will be 2 units will be optimal.  

Editor Image

?