Minimum Operations to Make Strings Equal

Given two strings A and B consisting of lowercase English letters, determine the minimum number of pre-processing moves required on string A to make it equal to string B using the following 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 pre-process 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 pre-process move does not matter. 2. You cannot apply pre-process moves to the string B or make any pre-process moves after the first change is made.

Input:

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:

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.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • Where N is the length of the strings.
  • Time Limit: 1 sec
AnswerBot
4mo

The minimum number of pre-processing moves required on string A to make it equal to string B using specified operations.

  • Iterate through both strings and count the number of differences between corresp...read more

Help your peers!
Select
Add answer anonymously...
Billfree Labs Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits