
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:
- Choose any index
i
(0 <=i
<n
) and swap charactersa[i]
andb[i]
. - Choose any index
i
(0 <=i
<n
) and swap charactersa[i]
anda[n-i-1]
. - Choose any index
i
(0 <=i
<n
) and swap charactersb[i]
andb[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.


Determine the minimum number of pre-processing moves required to make two strings equal by swapping characters and replacing characters in one string.
Iterate through both strings simultaneously and co...read more


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

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
Top Wipro Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month