Given N binary strings b1, b2, b3…. bn. The task is to find the maximum number of binary strings that you can make palindromic by swapping any pair of characters any number of times. Characters can be either from the same string or from different strings
Input: N=3 1110 100110 010101 Output: 2 Explanation: b1 = 1110 b2 = 100110 - > 110010 b3 = 010101 -> 110010 Now swap last 0 in s2 and s3 with 1's in s1 Final string become b1 = 1000 b2 = 110011 b3 = 110011 where b1 and b2 are a palindrome Input: N=3 1 1000 111110 Output: 3
The lengths of the strings don’t change. It is also important to observe that if we are given a binary string of odd length, then we can always swap the characters in such a way to convert that string to be palindromic. This is because if the length is odd then we will have either (even numbers of zeros and an odd number of ones) or(even no of ones and an odd number of zeros). So it can always be placed in such a way to make that string palindromic.
Now as our ans can be either N or N-1. We have to think about the cases when our ans will be N. So, we are given N binary strings. If there is at least 1 string of odd length, then our ans will surely be N.
This can be explained as follows:
- Grab all the 0’s and 1’s and remove them from their spots. Then we will have at least a pair of 0’s or 1’s and then place them into their free spots symmetrically (skipping the middles of odd length). So by now, all the strings of even length are filled and strings of odd length have a free spot in the middle which can be easily filled with remaining characters. So in this case, our ans will be N.
- Now, Another case. If we have all of the N strings of even length individually and total no of 1’s and 0’s are even (i.e total count of 1’s are even and the total count of 0’s are even), then in this case also our ans will be N. This is because 1’s and 0’s can be placed symmetrically in all of the N strings to make them palindrome. Otherwise, our ans will be N-1.
Below is the implementation of the above approach:
- Rearrange the string to maximize the number of palindromic substrings
- Number of strings of length N with no palindromic sub string
- Find all palindromic sub-strings of a given string | Set 2
- Check if all the palindromic sub-strings are of odd length
- Find all distinct palindromic sub-strings of a given string
- Check if K palindromic strings can be formed from a given string
- Count of non-palindromic strings of length M using given N characters
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Count of unique palindromic strings of length X from given string
- Print all possible palindromic string formed using any pair of given strings
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Longest palindromic String formed using concatenation of given strings in any order
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Count of pairs of strings whose concatenation forms a palindromic string
- Make palindromic string non-palindromic by rearranging its letters
- Palindromic divisors of a number
- Number of palindromic permutations | Set 1
- Number of common base strings for two strings
- Longest Palindromic Substring using Palindromic Tree | Set 3
Improved By : AnkitRai01