Minimum operations to make strings equal

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
72 upvotes
Asked in companies
BillFree LabsCapegemini Consulting India Private Limited

Problem statement

You have been given two strings A and B consisting of lower case English letters. The task is to count the minimum number of pre-processing moves on the string A required to make it equal to string B after applying below operations:

1. Choose any index i (0 <= i < n) and swap characters a[i]  and b[i].
2. Choose any index i (0 <= i < n) and swap characters a[i]  and a[n-i-1] .
3. Choose any index i (0 <= i < n) and swap characters b[i]  and b[n-i-1] .

In one preprocess move, you can replace a character in A with any other character of the English alphabet.

Note:
1. The number of changes you make after the preprocess move does not matter.
2. You cannot apply to preprocess moves to the String B or make any preprocess moves after the first change is made.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The first line of each test case contains string A consisting of lowercase English letters.

The second line of each test case contains string B consisting of lowercase English letters.
Output Format:
For each test case, print a single integer denoting the minimum number of pre-processing moves on the string A required to make it equal to the string B. Print -1, if it is impossible to make strings equal.

The output for each test case is in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given fuction.
Constraints:
1 <= T <= 100
1 <= N <= 5000

Where ‘N’ is the length of the strings.

Time Limit: 1 sec
Sample Input 1:
2
abacaba
bacabaa
zcabd
dbacz
Sample Output 1:
4
0
Explanation of Sample Input 1:
For the first test case, preprocess moves are as follows: A[0] = ‘b’, A[2] = ‘c’, A[3] = ‘a’ and A[4] = ‘b’. Afterwards, A = ‘“bbcabba”. Then we can obtain equal strings by the following sequence of changes: swap(A[2], B[2]) and swap(A[2], A[6]). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make strings equal, so the answer in this test case is 4.

For the second test case, no preprocess moves are required. We can use the following sequence of changes to make A and B equal: swap(B[1], B[5]), swap(A[2], A[4]).
Sample Input 2:
3
zxyyxzx
zyzyxyy
jfhjfl
jhkkjs
abcd
abcde
Sample Output 2:
3
4
-1
Hint

Can you think about grouping the interchangeable characters and process them?

Approaches (2)
Using HashMap

Let’s divide all characters of both strings into groups in such a way that characters in each group can be swapped with each other with changes. So, there will be the following groups: {'A'[0], ‘A’['N'-1], ‘B’[0], ‘B’['N'-1]}, {'A'[1], ‘A’['N'-2], ‘B’[1], ‘B’['N'-2]} and so on... Since these groups don’t affect each other, we can calculate the number of pre-processing moves in each group and then sum it up.

 

How to determine if a group does not need any pre-processing moves?

For a group consisting of ‘2’ characters (there will be one such group if ‘N’ is odd), that’s easy - If the characters in this group are equal, the answer is ‘0’; otherwise, it’s ‘1’.

 

To determine the required number of pre-processing moves for a group consisting of four characters, we may use the following fact: this group doesn’t require any pre-processing moves if the characters in this group can be divided into pairs. So if the group contains four equal characters or two pairs of equal characters, then the answer for this group is 0. Otherwise, we may check that replacing only one character of ‘A’[i] and ‘A’['N' - 'i' - 1] will be enough. If so, then the answer is ‘1’; otherwise, it’s ‘2’.

 

We will use HashMap to count the number of distinct characters in a particular group. The key in the HashMap will be the character itself, and the value corresponding to that key will be its frequency.

Time Complexity

O(N), where ‘N’ is the length of the given strings.

 

Since we are traversing in the given string only once, also, insertion and searching operations take constant time into the HashMap. So, overall time complexity will be O(N).

Space Complexity

O(1), i.e. constant space complexity.

 

Since no extra memory is used except a HashMap of size 4, which is constant with respect to the length of the string, so, overall space complexity will be constant.

Code Solution
(100% EXP penalty)
Minimum operations to make strings equal
Full screen
Console