![]() |
VOOZH | about |
Given a string S consisting of lowercase characters, the task is to find the minimum number of swaps required in the string such that S[i] != S[n-i+1], where 1 <= i <= n.
Examples:
Input: N = 3, S = "ABC"
Output: -1
Explanation: In the string "abc" , s[2] = s[3-2+1] even after applying the allowed operation.Input: N = 10, S = "taarrrataa"
Output: 1
Explanation: It is enough to swap the 2nd and 5th characters of the string "taarrrataa" and the new string "trararataa" satisfies the given condition.
Approach: To solve the problem follow the below idea:
If the string has an odd number of letters, you can't make any solution. This is because there will always be a middle letter that can't be paired with anything else. If the string has an even number of letters, you have to do some operations
Below are steps to solve the problem:
Below is the implementation of the above approach:
1
Time Complexity: O(N)
Auxiliary Space: O(1)