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)
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.
- Program to find the largest and smallest ASCII valued characters in a string
- Hexadecimal equivalents in Binary Valued Graph
- Octal equivalents of connected components in Binary valued graph
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- 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
- Check if frequency of each character is equal to its position in English Alphabet
- Smallest alphabet greater than a given character
- Count characters at same position as in English alphabet
- Latin alphabet cipher
- Most frequent word in first String which is not present in second String
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Find the most frequent digit without using array/string
- Find lexicographically smallest string in at most one swaps
- Count of strings that can be formed from another string using each character at-most once
- Lexicographically smallest string formed by removing at most one character
- Length of longest consecutive ones by at most one swap in a Binary 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 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.