
- 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
Maximum even length sub-string that is permutation of a palindrome in C++
Problem statement
Given a string the task is to find the maximum length of the sub-string of that can be arranged into a Palindrome.
Example
If input string = “5432112356” then answer is 6 as maximum palindrome substring is “321123” and its length is 6
Algorithm
- If the length of the sub-string is odd, then it cannot be considered in the final solutions.
- If the length of the sub-string is even, then it can be a possible solution only if each character in that sub-string occurs even number of times which can be done using the dictionary count. We check if each character occurs even number of times or not. If yes, then we include it as one of the possible solutions. Then we form the next sub-string by including the next character in the string which can be done by simply incrementing end and check recursively if a sub-string with greater length can be formed which satisfies the given conditions and return the maximum of all possible solutions
Example
#include <bits/stdc++.h> using namespace std; unordered_map<int, int> countt; bool isPalindromePossible(unordered_map<int, int> &countt) { for (auto key : countt) { if (key.second & 1) { return false; } return true; } int getMaxPalindrome(string str, unordered_map<int, int> &countt, int start, int end) { if (end == str.length()) { if ((end - start) % 2 == 0) if (isPalindromePossible(countt)) return end - start; return 0; } else { if ((end - start) % 2 == 0) { if (isPalindromePossible(countt)) { countt[str[end]]++; return max(end - start, getMaxPalindrome(str, countt, start, end + 1)); } else { countt[str[end]]++; return getMaxPalindrome(str, countt, start, end + 1); } } else { countt[str[end]]++; unordered_map<int, int> c(countt.begin(), countt.end()); int length = getMaxPalindrome(str, c, start, end + 1); countt[str[end]]--; countt[str[start]]--; return max(length, getMaxPalindrome(str, countt, start + 1, end)); } } } int main(int argc, char const *argv[]) { string str = "5432112356"; int start = 0, end = 0; cout << "Maximum palindrome length = " << getMaxPalindrome(str, countt, start, end) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Maximum palindrome length = 6
- Related Articles
- Check if a string contains a palindromic sub-string of even length in C++
- Check if a string contains a palindromic sub-string of even length in Python
- Count all Palindrome Sub-Strings in a String in C++
- Maximum length of subarray such that sum of the subarray is even in C++
- Find the first maximum length even word from a string in C++
- Palindrome Permutation II in C++
- Maximum length of a sub-array with ugly numbers in C++
- Find the Nth Even Length Palindrome using C++
- Checking for permutation of a palindrome in JavaScript
- Minimum removal to make palindrome permutation in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- How to replace a sub-string with the reverse of that sub-string in R?
- What is the maximum length of a string in Python?
- Find the maximum value permutation of a graph in C++

Advertisements