Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find longest palindrome formed by removing or shuffling chars from string in Python
Suppose we have a string; we have to find the longest palindrome that can be generated by deleting or shuffling the characters from the string. If there are more than one palindrome, then return only one.
So, if the input is like pqqprrs, then the output will be pqrsrqp.
Approach
To solve this problem, we will follow these steps ?
Count the frequency of each character in the string
For each character, use pairs to form the left and right sides of the palindrome
If any character has an odd count, use one instance as the middle character
Construct the palindrome by combining left + middle + right (reversed)
Algorithm Steps
count := array of size 256, filled with 0
-
for i in range 0 to size of string, do
count[ASCII of(string[i])] := count[ASCII of(string[i])] + 1
begin := blank string, mid := blank string, end := blank string
character := ASCII of('a')
-
while character ≤ ASCII of('z'), do
-
if count[character] is odd, then
mid := character
count[character] := count[character] - 1
-
for i in range 0 to count[character]/2, do
begin := begin + character
character := character + 1
-
end := reverse of begin
return begin + mid + end
Example
Let us see the following implementation to get better understanding ?
def get_palindrome(string):
count = [0] * 256
# Count frequency of each character
for i in range(len(string)):
count[ord(string[i])] += 1
begin = ""
mid = ""
character = ord('a')
# Process each character from 'a' to 'z'
while character <= ord('z'):
# If character has odd count, use one for middle
if (count[character] & 1):
mid = chr(character)
count[character] -= 1
# Use pairs for left side of palindrome
for i in range(count[character] // 2):
begin += chr(character)
character += 1
# Create palindrome: left + middle + right
end = begin[::-1]
return begin + mid + end
# Test the function
string = "pqqprrs"
result = get_palindrome(string)
print(f"Input: {string}")
print(f"Longest palindrome: {result}")
The output of the above code is ?
Input: pqqprrs Longest palindrome: pqrsrqp
How It Works
For the input pqqprrs:
Character counts: p=2, q=2, r=2, s=1
Characters with even counts (p, q, r): use half for left side ? "pqr"
Character with odd count (s): use one for middle ? "s"
Right side is reverse of left side ? "rqp"
Final palindrome: "pqr" + "s" + "rqp" = "pqrsrqp"
Alternative Implementation
Here's a more concise version using Python's Counter ?
from collections import Counter
def get_palindrome_v2(string):
count = Counter(string)
left = ""
middle = ""
# Process characters in sorted order
for char in sorted(count.keys()):
freq = count[char]
# Add pairs to left side
left += char * (freq // 2)
# Use first odd-count character as middle
if freq % 2 == 1 and middle == "":
middle = char
# Build palindrome
right = left[::-1]
return left + middle + right
# Test both implementations
string = "pqqprrs"
result1 = get_palindrome(string)
result2 = get_palindrome_v2(string)
print(f"Method 1: {result1}")
print(f"Method 2: {result2}")
The output of the above code is ?
Method 1: pqrsrqp Method 2: pqrsrqp
Conclusion
To find the longest palindrome from a string, count character frequencies and use pairs for the sides and one odd-count character for the middle. This approach ensures maximum length while maintaining palindrome properties.
---