Minimum operations to make strings equal

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.
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
CodingNinjas
author
2y
Using Variables

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: ...read more

CodingNinjas
author
2y
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: {'...read more

Shivam Dubey
1y
10
Rathod Vinod
2y

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...read more

Anonymous
2y

Using Variables

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:...read more

Add answer anonymously...
Wipro Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter