Given a string str containing lowercase characters, the task is to find the lexicographically largest sub-sequence of str.
Input: str = “abc”
All possible sub-sequences are “a”, “ab”, “ac”, “b”, “bc” and “c”
and “c” is the largest among them (lexicographically)
Input: str = “geeksforgeeks”
Approach: Let mx be the lexicographically largest character in the string. Since we want the lexicographically largest sub-sequence we should include all occurrences of mx. Now after all the occurrences have been used, the same process can be repeated for the remaining string (i.e. sub-string after the last occurrence of mx) and so on until the there are no more characters left.
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.
- Find the lexicographically largest palindromic Subsequence of a String
- Lexicographically largest subsequence such that every character occurs at least k times
- Lexicographically largest string possible in one swap
- Lexicographically largest string formed from the characters in range L and R
- Lexicographically smallest and largest substring of size k
- Lexicographically largest prime path from top-left to bottom-right in a matrix
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically next string
- Largest subsequence such that all indices and all values are multiples individually
- Lexicographically middle string
- Lexicographically n-th permutation of a string
- Lexicographically first palindromic string
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest string after M operations
- Find n-th lexicographically permutation of a string | Set 2
- Lexicographically minimum string rotation | Set 1
- Lexicographically next greater string using same character set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.