Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

LEXICOGRAPHICALLY FIRST

PALINDROMIC STRING
Lexicographically First Palindromic String

Introduction

Imagine you have been given a string of alphabets.

The task is to rearrange the characters of the given string to


form a lexicographically first palindromic string
Lexicographically First / Lexicographically Minimum

String x is lexicographically less than string y

If either x is a prefix of yOr,


(and x ≠ y)
There exists such i (1 ≤ i ≤ min(|x|, |y|)),
that xi < yi, and for any j (1 ≤ j < i) xj = yj
The lexicographic comparison of strings is implemented by operator < i
modern programming languages.
Lexicographically First Palindromic String

Our output string must be lexicographically minimum or


lexicographically first.
Example :
Sample Input : str = “aabcc”
Sample Output : str = “acbca”

Print -1, if there are no such lexicographically minimum substring.


Lexicographically First Palindromic String

Simple Approach

Sort the string characters in alphabetical(ascending) order.


One by one find lexicographically next permutation of the given

string.
The first permutation which is palindrome is the answer.
Lexicographically First Palindromic String
Algorithm
1. Store frequency of each character in the given string

2. Check whether a palindromic string can be formed or not using the

properties of palindromic string mentioned above.

3. If palindromic string cannot be formed, return “No Palindromic

String”.

4. Else we create three strings and then return;

front_str + odd_str + rear_str


Lexicographically First Palindromic
String
class EthCode { // count_odd to count no of
static char MAX_CHAR = 26; // chars with odd frequency
// Function to count frequency of each char in int count_odd = 0;
the for (int i = 0; i < MAX_CHAR; i++)
// string. freq[0] for 'a',...., freq[25] for {
'z' if (freq[i] % 2 != 0)
static void countFreq(String str, int freq[], {
int len) count_odd++;
{ }
for (int i = 0; i < len; i++) }
{ // For even length string
freq[str.charAt(i) - 'a']++; // no odd freq character
} if (len % 2 == 0)
} {
// Cases to check whether a palindr0mic if (count_odd > 0)
// string can be formed or not { return false;
static boolean canMakePalindrome(int freq[], }
int len) else
{ { return true;
} }
Lexicographically First Palindromic String
// For odd length string freq[i]--;
// one odd freq character odd_str = odd_str + (char) (i + 'a');
if (count_odd != 1) return odd_str;
{ }
return false; }
} return odd_str;
return true; }
} // To find lexicographically first
// Function to find odd freq char and palindromic
// reducing its freq by 1returns "" if odd freq // string.
// char is not present static String findPalindromicString(String
static String findOddAndRemoveItsFreq(int str)
freq[]) {
{ int len = str.length();
String odd_str = ""; int freq[] = new int[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++) countFreq(str, freq, len);
{ if (!canMakePalindrome(freq, len))
if (freq[i] % 2 != 0) {
{ return "No Palindromic String";
}
// Assigning odd freq character if present // creating front string
// else empty string. front_str = front_str + temp;
String odd_str = // creating rear string
findOddAndRemoveItsFreq(freq); rear_str = temp + rear_str;
String front_str = "", rear_str = " "; }
// Traverse characters in increasing order }
for (int i = 0; i < MAX_CHAR; i++) // Final palindromic string which is
{ // lexicographically first
String temp = ""; return (front_str + odd_str + rear_str);
if (freq[i] != 0) }
{
char ch = (char) (i + 'a'); public static void main(String[] args)
// Divide all occurrences into two {
// halves. Note that odd character String str = "malayalam";
// is removed by findOddAndRemoveItsFreq()
for (int j = 1; j <= freq[i] / 2; j++) System.out.println(findPalindromicString(s
{ tr));
temp = temp + ch; }
} }
Lexicographically First Palindromic String

Time Complexity : O(n)

Auxiliary Space Complexity : Constant


THANK YOU

You might also like