Maximize the number of palindromic Strings
Last Updated :
20 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int max_palindrome(string s[], int n)
{
int flag = 0;
for ( int i = 0; i < n; i++) {
if (s[i].size() % 2 != 0) {
flag = 1;
}
}
if (flag == 1) {
return n;
}
int z = 0, o = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < s[i].size(); j++) {
if (s[i][j] == '0' )
z++;
else
o++;
}
}
if (o % 2 == 0 && z % 2 == 0) {
return n;
}
else {
return n - 1;
}
}
int main()
{
int n = 3;
string s[n] = { "1110" , "100110" , "010101" };
cout << max_palindrome(s, n);
return 0;
}
|
Java
class GFG
{
static int max_palindrome(String []s, int n)
{
int flag = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s[i].length() % 2 != 0 )
{
flag = 1 ;
}
}
if (flag == 1 )
{
return n;
}
int z = 0 ;
int o = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < s[i].length(); j++)
{
if (s[i].charAt(j) == '0' )
z += 1 ;
else
o += 1 ;
}
}
if (o % 2 == 0 && z % 2 == 0 )
{
return n;
}
else
{
return n - 1 ;
}
}
public static void main (String[] args)
{
int n = 3 ;
String s[] = { "1110" , "100110" , "010101" };
System.out.println(max_palindrome(s, n));
}
}
|
Python3
def max_palindrome(s, n) :
flag = 0 ;
for i in range (n) :
if ( len (s[i]) % 2 ! = 0 ) :
flag = 1 ;
if (flag = = 1 ) :
return n;
z = 0 ; o = 0 ;
for i in range (n) :
for j in range ( len (s[i])) :
if (s[i][j] = = '0' ) :
z + = 1 ;
else :
o + = 1 ;
if (o % 2 = = 0 and z % 2 = = 0 ) :
return n;
else :
return n - 1 ;
if __name__ = = "__main__" :
n = 3 ;
s = [ "1110" , "100110" , "010101" ];
print (max_palindrome(s, n));
|
C#
using System;
class GFG
{
static int max_palindrome( string []s, int n)
{
int flag = 0;
for ( int i = 0; i < n; i++)
{
if (s[i].Length % 2 != 0)
{
flag = 1;
}
}
if (flag == 1)
{
return n;
}
int z = 0;
int o = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < s[i].Length; j++)
{
if (s[i][j] == '0' )
z += 1;
else
o += 1;
}
}
if (o % 2 == 0 && z % 2 == 0)
{
return n;
}
else
{
return n - 1;
}
}
public static void Main ()
{
int n = 3;
string []s = { "1110" , "100110" , "010101" };
Console.WriteLine(max_palindrome(s, n));
}
}
|
Javascript
<script>
function max_palindrome(s, n)
{
let flag = 0;
for (let i = 0; i < n; i++)
{
if (s[i].length % 2 != 0)
{
flag = 1;
}
}
if (flag == 1)
{
return n;
}
let z = 0;
let o = 0;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < s[i].length; j++)
{
if (s[i][j] == '0 ')
z += 1;
// Count of 1' s in
else
o += 1;
}
}
if (o % 2 == 0 && z % 2 == 0)
{
return n;
}
else
{
return n - 1;
}
}
let n = 3;
let s = [ "1110" , "100110" , "010101" ];
document.write(max_palindrome(s, n));
</script>
|
Time Complexity: O(n * |w|), where w is max length of word in string s
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...