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
- 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 | Set 2
- Find the longest sub-string which is prefix, suffix and also present inside the string
- 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 length of longest subsequence of one string which is substring of another string
- Find the count of palindromic sub-string of a string in its sorted form
- Find the character in first string that is present at minimum index in second string
- Queries to find the first non-repeating character in the sub-string of a string
- Find Kth largest string from the permutations of the string with two characters
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.