# Make a lexicographically smallest palindrome with minimal changes

Given a string S. Print the lexicographically smallest string possible. You can make minimal changes to the characters in the string and you can permute the string.

Examples:

Input : S = "aabc" Output : "abba" Input : S = "äabcd" Output : "abcba"

**Explanation 1:**Change the last index “c” to “b”, it becomes “aabb”. Then rearrange the string, it becomes “abba”. This is the lexicographically smallest string.

**Explanation 2:** change “d” to “b” => “aabcb” => “abcba”.

**Approach:**

cnt[a] be the no.of occurences of the character a. Consider odd values of cnt[a], then a palindrome can not contain more than one character a with cnt[a]. Denote characters with odd count cnt[] as follows a_{1}, a_{2}….a_{k}(in alphabetical order). Replace any of the symbols a_{k} with a_{1} and a_{k-1} with a_{2} and so on upto middle of sequence above. Now, there are no more than one odd symbol. Place it in the middle of answer. First half of answer will consist of occurences of symbol a. Second half will contains same symbols in reverse order.

**Example :**

take string S = "aabcd" cnt[a] = 2, cnt[b] = 1, cnt = 1, cnt[d] = 1. Odd characters => b, c, d replace a_{k}('d') with a_{1}('b') we get => b, c, b. cnt[b] = 2, cnt = 1.Place odd character in middle and 'a' and 'b' in first half and also in second half, we get "abcba".

## C++

`// CPP code for changing a string ` `// into lexicographically smallest ` `// pallindromic string ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to create a palindrome ` `int` `Palindrome(string s, ` `int` `n) ` `{ ` ` ` `unordered_map<` `char` `, ` `int` `> cnt; ` ` ` `string R = ` `""` `; ` ` ` ` ` `// Count the occurences of ` ` ` `// every character in the string ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `char` `a = s[i]; ` ` ` `cnt[a]++; ` ` ` `} ` ` ` ` ` `// Create a string of characters ` ` ` `// with odd occurences ` ` ` `for` `(` `char` `i = ` `'a'` `; i <= ` `'z'` `; i++) { ` ` ` `if` `(cnt[i] % 2 != 0) ` ` ` `R += i; ` ` ` `} ` ` ` ` ` `int` `l = R.length(); ` ` ` `int` `j = 0; ` ` ` ` ` `// Change the created string upto ` ` ` `// middle element and update ` ` ` `// count to make sure that only ` ` ` `// one odd character exists. ` ` ` `for` `(` `int` `i = l - 1; i >= l / 2; i--) { ` ` ` ` ` `// decrease the count of ` ` ` `// character updated ` ` ` `cnt[R[i]]--; ` ` ` `R[i] = R[j]; ` ` ` `cnt[R[j]]++; ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Create three strings to make ` ` ` `// first half second half and ` ` ` `// middle one. ` ` ` `string first, middle, second; ` ` ` ` ` `for` `(` `char` `i = ` `'a'` `; i <= ` `'z'` `; i++) { ` ` ` `if` `(cnt[i] != 0) { ` ` ` ` ` `// characters with even occurrences ` ` ` `if` `(cnt[i] % 2 == 0) { ` ` ` `int` `j = 0; ` ` ` ` ` `// fill the first half. ` ` ` `while` `(j < cnt[i] / 2) { ` ` ` `first += i; ` ` ` `j++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// character with odd occurrence ` ` ` `else` `{ ` ` ` `int` `j = 0; ` ` ` ` ` `// fill the first half with ` ` ` `// half of occurrence except one ` ` ` `while` `(j < (cnt[i] - 1) / 2) { ` ` ` `first += i; ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// For middle element ` ` ` `middle += i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// create the second half by ` ` ` `// reversing the first half. ` ` ` `second = first; ` ` ` `reverse(second.begin(), second.end()); ` ` ` `string resultant = first + middle + second; ` ` ` `cout << resultant << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string S = ` `"geeksforgeeks"` `; ` ` ` `int` `n = S.length(); ` ` ` `Palindrome(S, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

eefgksoskgfee

## Recommended Posts:

- Make lexicographically smallest palindrome by substituting missing characters
- Construct lexicographically smallest palindrome
- Minimal operations to make a number magical
- Replace minimal number of characters to make all characters pair wise distinct
- Lexicographically smallest rotated sequence | Set 2
- Lexicographically smallest permutation of {1, .. n} such that no. and position do not match
- Lexicographically smallest and largest substring of size k
- Lexicographically Kth smallest way to reach given coordinate from origin
- Lexicographically smallest permutation of a string with given subsequences
- Queries to answer the X-th smallest sub-string lexicographically
- K-th lexicographically smallest unique substring of a given string
- Order of indices which is lexicographically smallest and sum of elements is <= X
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest array after at-most K consecutive swaps
- Lexicographically smallest substring with maximum occurrences containing a's and b's only

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.