0% found this document useful (0 votes)
32 views18 pages

Fall Semester 2024-25 - STS3007 - TH - AP2024252001241 - 2024-10-01 - Reference-Material-I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views18 pages

Fall Semester 2024-25 - STS3007 - TH - AP2024252001241 - 2024-10-01 - Reference-Material-I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LEXICOGRAPHICALL

Y FIRST
PALINDROMIC
STRING
TEST TIME ON MAJORITY ELEMENT

URL:https://forms.gle/SFDVg26roZ2rtzcy5

QR CODE:
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 y (and x ≠ y) Or,

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 < in


modern programming languages​​.
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.


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.


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_st + rear_str


r
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' {
static void countFreq(String str, int freq[], int if (freq[i] % 2 != 0)
len) {
{ count_odd++;
for (int i = 0; i < len; i++) }
{ }
freq[str.charAt(i) - 'a']++; // For even length string
} // no odd freq character
} if (len % 2 == 0)
// Cases to check whether a palindr0mic {
// string can be formed or not if (count_odd > 0)
static boolean canMakePalindrome(int freq[], { return false;
int len) }
{ else
{ return true;
}}
// 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 palindromic
} // string.
// Function to find odd freq char and static String findPalindromicString(String
// reducing its freq by 1returns "" if odd freq// str)
char is not present {
static String findOddAndRemoveItsFreq(int int len = str.length();
freq[]) int freq[] = new int[MAX_CHAR];
{ countFreq(str, freq, len);
String odd_str = ""; if (!canMakePalindrome(freq, len))
for (int i = 0; i < MAX_CHAR; i++) {
{ return "No Palindromic String";
if (freq[i] % 2 != 0) }
{
// 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(st
{ r));
temp = temp + ch; }}
}
Time Complexity : O(n)

Auxiliary Space Complexity : Constant


Interview questions

1.Explain the problem statement of the "Lexicographically First


Palindromic String" problem.

The problem involves finding the smallest palindromic string that can
be formed using the characters of a given string `s`, and the resulting
string should be lexicographically smallest among all possible
palindromic strings.
Interview questions
2.How does the proposed solution work?

The solution sorts the characters of the input string, and then iterates
through the sorted characters. It appends each character to both the
beginning and the end of the resulting palindromic string. This way, it
constructs a palindromic string in a way that maintains lexicographical
order.
Interview questions

3. Can you explain the time complexity of the solution?

The time complexity is dominated by the sorting step, which takes O(n
* log n) time, where n is the length of the input string. The subsequent
iteration through the sorted characters takes O(n) time. So, the overall
time complexity is O(n * log n).
Interview questions

4. What happens if the input string contains duplicate characters?

Duplicate characters are handled correctly in the solution. The sorting


step ensures that duplicate characters are grouped together, and the
iteration through the sorted characters appends each character twice,
maintaining their duplicates in the resulting palindromic string.
https://learn.codemithra.com
THANK
YOU

+91 78150 [email protected] www.codemithra.com


95095

You might also like