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 smallest subsequence possible by removing a character from given string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically largest string for given dictionary order
- Lexicographically largest string possible for a given cost of appending characters
- Lexicographically largest string formed from the characters in range L and R
- Lexicographically largest possible String after removal of K characters
- Lexicographically largest string possible in one swap
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- 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 first K characters of a string | Set 2
- Lexicographically smallest permutation of a string that contains all substrings of another string
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest permutation of a string with given subsequences
- Find the lexicographically smallest string which satisfies the given condition
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.