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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- For each lowercase English alphabet find the count of strings having these alphabets
- Hexadecimal equivalents in Binary Valued Graph
- Octal equivalents of connected components in Binary valued graph
- Latin alphabet cipher
- Smallest alphabet greater than a given character
- Find the longest sub-string which is prefix, suffix and also present inside the string
- 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 count of palindromic sub-string of a string in its sorted form
- 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
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Count characters at same position as in English alphabet
- Queries to find the last non-repeating character in the sub-string of a given 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
- Find the smallest window in a string containing all characters of another 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.