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 to make string A 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-processing move, you can replace a character in A with any other character of the English alphabet.

Input:

The first line contains an integer T representing the number of test cases. Each test case consists of two lines:
First line: String A
Second line: String B

Output:

For each test case, output a single integer denoting the minimum number of pre-processing moves required to make string A equal to string B. Print -1 if it's impossible to achieve this. Output should be on a separate line for each test case.

Example:

Suppose T = 1, A = "abc", and B = "bca". The output should be:

2

Explanation: Two pre-processing moves can be performed to change 'a' to 'b' and 'c' to 'a'.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000

Note:

  • No pre-processing moves can be applied to string B.
  • You must initiate conversion only with pre-processing operations; no further changes post these moves.
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

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