Check if a string can be made palindromic by swapping pairs of characters from indices having unequal characters in a Binary String
Given a string S and a binary string B, both of length N, the task is to check if the given string S can be made palindromic by repeatedly swapping characters at any pair of indices consisting of unequal characters in the string B.
Input: S = “BAA”, B = “100”
Swapping S and S modifies S to “ABA” and B to “010”.
Input: S = “ACABB”, B = “00000”
Approach: Follow the below steps to solve this problem:
- Check if string S can be rearranged to form a palindromic string or not. If found to be false, then print “No”.
- Otherwise, if the string S is a palindrome, then print “Yes”.
- If the count of 0s and 1s is at least 1, then there always exists a way to swap characters to make the given string S palindromic. Therefore, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)