Given a string str, the task is to find the maximum valued alphabet in str. The value of a particular alphabet is defined as the difference in the indices of its last and the first occurrence. If there are multiple such alphabets then find the lexicographically smallest alphabet.
Input: str = “abbba”
value(‘a’) = 4 – 0 = 4
value(‘b’) = 3 – 1 = 2
Input: str = “bbb”
Approach: The idea is to store the first and the last occurrences of each of the alphabets in two auxiliary arrays say first and last. Now, these two arrays can be used to find the maximum valued alphabet in the given string.
Below is the implementation of the above approach:
Time Complexity: O(N)
- Program to find the largest and smallest ASCII valued characters in a string
- Find letter's position in Alphabet using Bit operation
- Find the Mid-Alphabet for each index of the given Pair of Strings
- Latin alphabet cipher
- Smallest alphabet greater than a given character
- Count characters at same position as in English alphabet
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- String Range Queries to find the number of subsets equal to a given String
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find length of longest subsequence of one string which is substring of another string
- Find the character in first string that is present at minimum index in second string
- Find the count of palindromic sub-string of a string in its sorted form
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the smallest window in a string containing all characters of another string
- Queries to find the first non-repeating character in the sub-string of a string
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.