Palindrome Permutation II in C++

C++Server Side ProgrammingProgramming

Suppose we have a string s, we have to find all the palindromic permutations of it, there will be no repetition. If no palindromic permutation is there, then simply return empty string.

So, if the input is like "aabb", then the output will be ["abba", "baab"]

To solve this, we will follow these steps −

  • Define an array ret

  • Define a function solve(), this will take s, sz, one unordered map m, idx initialize it with 0,

  • if sz is same as 0, then −

    • insert s at the end of ret

    • return

  • evenFound := false

  • Define one set visited

  • for each key-value pair it of m, do −

    • if value of it is zero, then −

      • (increase it by 1)

      • Ignore following part, skip to the next iteration

    • otherwise when value of it is same as 1, then −

      • oddChar := key of it

    • Otherwise

      • if key of it is not visited, then

        • Ignore following part, skip to the next iteration

      • s[idx] := key of it

      • s[size of s - 1 - idx] = key of it

      • evenFound := true

      • m[key of it] := m[key of it] - 2

      • solve(s, sz - 2, m, idx + 1)

      • m[key of it] := m[key of it] + 2

      • insert key of it into visited

    • (increase it by 1)

  • if evenFound false, then −

    • s[idx] := oddChar

    • solve(s, sz - 1, m, idx + 1)

  • From the main method do the following −

  • Define one map cnt

  • n := size of s

  • temp := blank string

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • (increase cnt[s[i]] by 1)

    • temp := temp concatenate " * "

  • oddCnt := 0

  • for each key-value pair in cnt, do −

    • increase oddCount when value of it is odd

    • (increase it by 1)

  • if oddCnt > 1, then −

    • return ret

  • solve(temp, n, cnt)

  • return ret

Example 

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto< v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
public:
   vector<string> ret;
   void solve(string s, int sz, unordered_map<char,int>& m, int idx = 0){
      if (sz == 0) {
         ret.push_back(s);
         return;
      }
      bool evenFound = false;
      char oddChar;
      unordered_map<char, int>::iterator it = m.begin();
      set<char> visited;
      while (it != m.end()) {
         if (!it->second) {
            it++;
            continue;
         }
         else if (it->second == 1) {
            oddChar = it->first;
         }
         else {
            if (visited.count(it->first))
               continue;
            s[idx] = it->first;
            s[s.size() - 1 - idx] = it->first;
            evenFound = true;
            m[it->first] -= 2;
            solve(s, sz - 2, m, idx + 1);
            m[it->first] += 2;
            visited.insert(it->first);
         }
         it++;
      }
      if (!evenFound) {
         s[idx] = oddChar;
         solve(s, sz - 1, m, idx + 1);
      }
   }
   vector<string< generatePalindromes(string s){
      unordered_map<char,int> cnt;
      int n = s.size();
      string temp = "";
      for (int i = 0; i < n; i++) {
         cnt[s[i]]++;
         temp += "*";
      }
      int oddCnt = 0;
      unordered_map<char, int>::iterator it = cnt.begin();
      while (it != cnt.end()) {
         oddCnt += (it->second & 1);
         it++;
      }
      if (oddCnt > 1)
         return ret;
      solve(temp, n, cnt);
      return ret;
   }
};
main(){
   Solution ob;
   print_vector(ob.generatePalindromes("aabb"));
}

Input

"aabb"

Output

[baab, abba, ]
raja
Published on 18-Nov-2020 16:11:18
Advertisements