
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Palindrome Permutation II in C++
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 −
#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, ]
- Related Articles
- Minimum removal to make palindrome permutation in C++
- Checking for permutation of a palindrome in JavaScript
- Palindrome Partitioning II in C++
- Maximum even length sub-string that is permutation of a palindrome in C++
- Next Permutation in Python
- Permutation Sequence in C++
- Find Permutation in C++
- Permutation in String in C++
- Permutation and Combination in Java
- Lexicographically next permutation in C++
- Permutation and Combination in Python?
- Letter Case Permutation in C++
- Count Vowels Permutation in C++
- Palindrome in Python: How to check a number is palindrome?
- What is Initial Permutation in DES?
