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
Examples:
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
Approach:
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.
- 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:
C++
// C++ program for the // above approach #include <bits/stdc++.h> using namespace std; int max_palindrome(string s[], int n) { int flag = 0; for ( int i = 0; i < n; i++) { // To check if there is // any string of odd length if (s[i].size() % 2 != 0) { flag = 1; } } // If there is at least // 1 string of odd // length. if (flag == 1) { return n; } int z = 0, o = 0; // If all the strings are // of even length. for ( int i = 0; i < n; i++) { for ( int j = 0; j < s[i].size(); j++) { // Count of 0's in all // the strings if (s[i][j] == '0' ) z++; // Count of 1's in // all the strings else o++; } } // If z is even // and o is even // then ans will be N. if (o % 2 == 0 && z % 2 == 0) { return n; } // Otherwise ans will be N-1. else { return n - 1; } } // Driver code int main() { int n = 3; string s[n] = { "1110" , "100110" , "010101" }; cout << max_palindrome(s, n); return 0; } |
Java
// Java program for the above approach class GFG { static int max_palindrome(String []s, int n) { int flag = 0 ; for ( int i = 0 ; i < n; i++) { // To check if there is // any string of odd length if (s[i].length() % 2 != 0 ) { flag = 1 ; } } // If there is at least // 1 string of odd // length. if (flag == 1 ) { return n; } int z = 0 ; int o = 0 ; // If all the strings are // of even length. for ( int i = 0 ; i < n; i++) { for ( int j = 0 ; j < s[i].length(); j++) { // Count of 0's in all // the strings if (s[i].charAt(j) == '0' ) z += 1 ; // Count of 1's in // all the strings else o += 1 ; } } // If z is even // and o is even // then ans will be N. if (o % 2 == 0 && z % 2 == 0 ) { return n; } // Otherwise ans will be N-1. else { return n - 1 ; } } // Driver code public static void main (String[] args) { int n = 3 ; String s[] = { "1110" , "100110" , "010101" }; System.out.println(max_palindrome(s, n)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 program for the above approach def max_palindrome(s, n) : flag = 0 ; for i in range (n) : # To check if there is # any string of odd length if ( len (s[i]) % 2 ! = 0 ) : flag = 1 ; # If there is at least # 1 string of odd # length. if (flag = = 1 ) : return n; z = 0 ; o = 0 ; # If all the strings are # of even length. for i in range (n) : for j in range ( len (s[i])) : # Count of 0's in all # the strings if (s[i][j] = = '0' ) : z + = 1 ; # Count of 1's in # all the strings else : o + = 1 ; # If z is even # and o is even # then ans will be N. if (o % 2 = = 0 and z % 2 = = 0 ) : return n; # Otherwise ans will be N-1. else : return n - 1 ; # Driver code if __name__ = = "__main__" : n = 3 ; s = [ "1110" , "100110" , "010101" ]; print (max_palindrome(s, n)); # This code is contributed by AnkitRai01 |
C#
// C# program for the above approach using System; class GFG { static int max_palindrome( string []s, int n) { int flag = 0; for ( int i = 0; i < n; i++) { // To check if there is // any string of odd length if (s[i].Length % 2 != 0) { flag = 1; } } // If there is at least // 1 string of odd // length. if (flag == 1) { return n; } int z = 0; int o = 0; // If all the strings are // of even length. for ( int i = 0; i < n; i++) { for ( int j = 0; j < s[i].Length; j++) { // Count of 0's in all // the strings if (s[i][j] == '0' ) z += 1; // Count of 1's in // all the strings else o += 1; } } // If z is even // and o is even // then ans will be N. if (o % 2 == 0 && z % 2 == 0) { return n; } // Otherwise ans will be N-1. else { return n - 1; } } // Driver code public static void Main () { int n = 3; string []s = { "1110" , "100110" , "010101" }; Console.WriteLine(max_palindrome(s, n)); } } // This code is contributed by AnkitRai01 |
Javascript
<script> // Javascript program for the above approach function max_palindrome(s, n) { let flag = 0; for (let i = 0; i < n; i++) { // To check if there is // any string of odd length if (s[i].length % 2 != 0) { flag = 1; } } // If there is at least // 1 string of odd // length. if (flag == 1) { return n; } let z = 0; let o = 0; // If all the strings are // of even length. for (let i = 0; i < n; i++) { for (let j = 0; j < s[i].length; j++) { // Count of 0's in all // the strings if (s[i][j] == '0 ') z += 1; // Count of 1' s in // all the strings else o += 1; } } // If z is even // and o is even // then ans will be N. if (o % 2 == 0 && z % 2 == 0) { return n; } // Otherwise ans will be N-1. else { return n - 1; } } let n = 3; let s = [ "1110" , "100110" , "010101" ]; document.write(max_palindrome(s, n)); // This code is contributed by divyeshrabadiya07. </script> |
2
Time Complexity: O(n * |w|), where w is max length of word in string s
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!