Check If a String Can Be Made Palindromic By Swapping Pairs Of Characters From Indices Having Unequal Characters In a Binary String


Problem Statement

We have given string str and a binary string B. The length of both strings is equal to the N. We need to check if we can make string str palindromic by swapping the characters of it multiple times at any pair of indices that contains unequal characters in string B.

Sample Examples

Input

str = ‘AAS’ B = ‘101’

Output

‘YES’

Explanation

We can swape the str[1] and str[2] as B[1] and B[2] are not equal. The final string can be ‘ASA’.

Input

str = ‘AASS’ B = ‘1111’

Output

‘No’

Explanation

We can’t make string palindrome as string B doesn’t contains unequal characters.

Input

str = ‘AASSBV’ B = ‘111100’

Output

‘No’

Explanation

We can’t make string str palindrome due to characters' frequency mismatch.

Approach 1

In the first approach, we will check if we can make any palindromic string using all the characters of string str. If yes, we can check if we can swap the characters at the pair of indexes containing the unequal character in string B and make the string palindromic. Otherwise, we return false.

Algorithm

  • Step 1 − Execute the utility() function that returns a boolean value based on whether the string can be palindromic by swapping characters according to the given conditions.

  • Step 2 − Define the canBePalindromic() function to check whether we can make any palindromic string using characters of str.

  • Step 2.1 − Create a map to store each character and its frequency in the string str. Use the for loop to iterate through the string and count the frequency of characters.

  • Step 2.2 − count the number of characters with even and odd frequencies.

  • Step 2.3 − Use the set to get the string's total number of unique characters.

  • Step 2.4 − Return true if the string length is odd and contains only 1 character with odd frequency.

  • Step 2.5 − If the string length is even, all characters with even frequency, and 0 characters with odd frequency, return true.

  • Step 2.6 − return false.

  • Step 3 − If the string can’t be palindromic, return false.

  • Step 4 − If string B contains more than one ‘1’ and ‘0’, return true; else, return false.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check if the string can be palindromic
bool canBePalindromic(string str){
   //Creating the map to store the frequency of each character
   map<char, int> charMap;
   // store the frequency of each character of string Str
   for (int i = 0; i < str.length(); i++) {
      charMap[str[i]] += 1;
   }
   // to store the count of even and odd frequent characters
   int even = 0, odd = 0;
   // iterate through the map
   for (auto e : charMap)  {
      //If frequency is odd, increment odd count; else, increment even count
      if (e.second % 2 == 1) {
         odd++;
      } else {
         even++;
      }
   }
   // set to store unique characters of string Str
   unordered_set<char> set;
   //Insert all characters of string Str in set
   for (int i = 0; i < str.size(); i++){
      set.insert(str[i]);
   }
   // if the string length is odd and only one character has an odd frequency, return true
   if (str.size() % 2 == 1 && even == set.size() - 1 && odd == 1){
      return true;
   }
   // If the string length is even and all characters have even frequency, return true
   if (str.size() % 2 == 0 && even == set.size() && odd == 0){
      return true;
   }
   // else return false
   return false;
}
// function to check if the string can be palindromic by swapping characters according to string B
bool utility(string S, string B){
   // If string S cannot be palindromic, return false.
   if (canBePalindromic(S) == false){
      return false;
   } else{
      // if at least one '1' and '0' is present in string B, string S can be palindromic
      int one = 0, zero = 0;
      for (int i = 0; i < B.size(); i++) {
         // If the current character is '0.'
         if (B[i] == '0'){
            zero++;
         } else {
            one++;
         }
      }
      // return true if at least one '1' and '0' is present in the string B
      if (one >= 1 && zero >= 1){
         return true;
      } else {
         return false;
      }
   }
}
int main(){
   string S = "NANA";
   string B = "0001";
   bool result = utility(S, B);
   if (result)
      cout << "Yes";
   else
      cout << "No";
   return 0;
}

Output

Yes
  • Time complexity − O(NlogN), as we use for loop to iterate through the string, and insert() method of set takes (logN) time.

  • Space complexity − O(K), where K is total number of unique characters.

Approach 2

In this approach, we will use an array to store the frequency of characters instead of a map.

Algorithm

  • Step 1 − Create a ‘charFrequancy’ array of length 26 and initialize with zero.

  • Step 2 − Count the total number of ones and zeros in the string B.

  • Step 3 − Update the frequency of every character in the array.

  • Step 4 − If the string length is even and the odd frequency is not zero, return false.

  • Step 5 − Return false if the string length is odd and the odd frequency is greater than 1.

  • Step 6 − if ones and zeros both exist in the string, return true.

  • Step 7 − return false.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check if the given string can be converted to a palindrome
bool utility(string str, string B){
   // array to store character counts in str
   int charFrequancy[26] = {0};
   int ones = 0, zeros = 0, odd_fre = 0;
   // count ones and zeros
   for (char ch : B) {
      if (ch == '1')
         ones++;
      if (ch == '0')
         zeros++;
   }
   // store counts of characters
   for (char ch : str){
      charFrequancy[ch - 'A']++;
   }
   // check total character with odd frequency
   for (int i = 0; i < 26; i++){
      if (charFrequancy[i] % 2 == 1)
         odd_fre++;
   }
   if (str.length() % 2 == 0 && odd_fre != 0)
      return false;
   if (str.length() % 2 == 1 && odd_fre > 1)
      return false;
   if (ones > 0 && zeros > 0)
      return true;
   return false;
}
int main(){
   string S = "NBCNB";
   string B = "01010";
   if (utility(S, B)){
      cout << "Yes";
   } else {
      cout << "No";
   }
   return 0;
}

Output

Yes
  • Time complexity − O(N), as we use the for loop to iterate through the string.

  • Space complexity − O(1), as we use the array of length 26 always.

Conclusion

We learned two approaches to check whether the string can be palindromic by swapping the characters according to the given condition. The first approach uses the set and map, and the second uses the array only to store data. The second approach is better than the first approach as the insert() method takes O(logn) time to insert data in the set, and the array takes O(1) time.

Updated on: 18-Jul-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements