Lexicographically First Palindromic String in JavaGiven a string "str," our task is to create a lexicographically first palindromic string by rearranging the characters in the given text. If there isn't a string of that type, then the message "no such palindromic string exists" will be returned. Example 1: Input: String str = "madam" Output: The lexicographic first palindromic string is amdma. Explanation: For the given string "madam", the lexicographic order of characters is {a, d, m}. So, we'll form the palindromic strings with the first character 'i'. Hence, the first lexicographical palindromic string that can be formed is "amdma". Example 2: Input: String str = "mississippi" Output: The lexicographic first palindromic string is iipssmsspii. Explanation: For the given string "mississippi", the lexicographic order of characters is {i, m, p, s}. So, we'll form the palindromic strings with the first character 'i'. Hence, the first lexicographical palindromic string that can be formed is "iipssmsspii". Example 3: Input: String str = "sis" Output: No such palindrome exists. Explanation: For the given string "sis", the lexicographic order of characters is {i, s, s}. Though the given string is a palindrome whe,n arranged in the lexicographic order, the string cannot be a palindrome. Hence no, such palindrome exists. Approach: Efficient ApproachThis approach finds the palindromic string, which particularly follows two properties: 1. Every character in the string must occur at an even frequency if the string's length is even. 2. In the event that the length is unique, there should be one character with an odd frequency, all other characters having an even frequency, and the odd character appearing at least once in the center of the string. Algorithm: Step 1: Create the countingFrequency function for calculating the frequency of each character in the input string. Step 2: Declare a function called palindrome to see if the input string's characters may be used to create a palindrome. Step 3: Initialize the function OddFrequency to identify and lessen the frequency of an odd character in the input string. Step 4: To create the lexicographically first palindromic string from the input string, create the primary function PalindromicString. Step 4.1: Use countingFrequency inside PalindromicString to determine each character's frequency. Step 5: Check whether a Palindrome can be formed or not by using the structure of the word. Step 6: Use OddFrequency to locate and, if possible, lower the frequency of an odd character. Step7: Create the front and back ends of the palindrome string by assembling the characters in ascending order. Step 8: Return the lexicographically initial palindromic string. Implementation:FileName: LexicographFirstPalindrome.java Output: The lexicographic first palindromic string is: amdma Complexity Analysis: The above code's time complexity is O(N), and its space complexity is O(N), where N represents the string's length. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India