Given a string . The task is to find the lexicographically largest subsequence of the string which is a palindrome.
Input : str = "abrakadabra" Output : rr Input : str = "geeksforgeeks" Output : ss
The idea is to observe a character a is said to be lexicographically larger than a character b if it’s ASCII value is greater than that of b.
Since the string has to be palindromic, the string should contain the largest characters only, as if we place any other smaller character in between the first and last character then it will make the string lexicographically smaller.
To find the lexicographically largest subsequence, first find the largest characters in the given string and append all of its occurrences in the original string to form the resultant subsequence string.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Lexicographically largest subsequence such that every character occurs at least k times
- Lexicographically first palindromic string
- Lexicographically all Shortest Palindromic Substrings from a given string
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Make palindromic string non-palindromic by rearranging its letters
- Lexicographically largest string formed from the characters in range L and R
- Lexicographically largest sub-sequence of the given string
- Lexicographically largest string possible in one swap
- Lexicographically largest possible String after removal of K characters
- Lexicographically largest string for given dictionary order
- Count All Palindromic Subsequence in a given String
- Largest palindromic string possible from given strings by rearranging the characters
- Find a palindromic string B such that given String A is a subsequense of B
- Find the count of palindromic sub-string of a string in its sorted form
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Lexicographically smallest and largest substring of size k
- Longest Palindromic Subsequence | DP-12
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.