VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-lexicographically-largest-palindromic-subsequence-of-a-string/

⇱ Find the lexicographically largest palindromic Subsequence of a String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the lexicographically largest palindromic Subsequence of a String

Last Updated : 15 Sep, 2022

Given a string . The task is to find the lexicographically largest subsequence of the string which is a palindrome.

Examples: 

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:


Output
ss

Complexity Analysis:

  • Time Complexity: O(N), where N is the length of the string.
  • Auxiliary Space: O(1)
Comment
Article Tags:
Article Tags: