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.

**Examples:**

Input:str = “abbba”

Output:a

value(‘a’) = 4 – 0 = 4

value(‘b’) = 3 – 1 = 2

Input:str = “bbb”

Output:b

**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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MAX = 26; ` ` ` `// Function to return the maximum ` `// valued alphabet ` `char` `maxAlpha(string str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the first and the last ` ` ` `// occurrence of all the characters ` ` ` `int` `first[MAX], last[MAX]; ` ` ` ` ` `// Set the first and the last occurrence ` ` ` `// of all the characters to -1 ` ` ` `for` `(` `int` `i = 0; i < MAX; i++) { ` ` ` `first[i] = -1; ` ` ` `last[i] = -1; ` ` ` `} ` ` ` ` ` `// Update the occurrences of the characters ` ` ` `for` `(` `int` `i = 0; i < len; i++) { ` ` ` ` ` `int` `index = (str[i] - ` `'a'` `); ` ` ` ` ` `// Only set the first occurrence if ` ` ` `// it hasn't already been set ` ` ` `if` `(first[index] == -1) ` ` ` `first[index] = i; ` ` ` ` ` `last[index] = i; ` ` ` `} ` ` ` ` ` `// To store the result ` ` ` `int` `ans = -1, maxVal = -1; ` ` ` ` ` `// For every alphabet ` ` ` `for` `(` `int` `i = 0; i < MAX; i++) { ` ` ` ` ` `// If current alphabet doesn't appear ` ` ` `// in the given string ` ` ` `if` `(first[i] == -1) ` ` ` `continue` `; ` ` ` ` ` `// If the current character has ` ` ` `// the highest value so far ` ` ` `if` `((last[i] - first[i]) > maxVal) { ` ` ` `maxVal = last[i] - first[i]; ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `(` `char` `)(ans + ` `'a'` `); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"abbba"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `cout << maxAlpha(str, len); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `static` `int` `MAX = ` `26` `; ` ` ` `// Function to return the maximum ` `// valued alphabet ` `static` `char` `maxAlpha(String str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the first and the last ` ` ` `// occurrence of all the characters ` ` ` `int` `[]first = ` `new` `int` `[MAX]; ` ` ` `int` `[]last = ` `new` `int` `[MAX]; ` ` ` ` ` `// Set the first and the last occurrence ` ` ` `// of all the characters to -1 ` ` ` `for` `(` `int` `i = ` `0` `; i < MAX; i++) ` ` ` `{ ` ` ` `first[i] = -` `1` `; ` ` ` `last[i] = -` `1` `; ` ` ` `} ` ` ` ` ` `// Update the occurrences of the characters ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) ` ` ` `{ ` ` ` ` ` `int` `index = (str.charAt(i) - ` `'a'` `); ` ` ` ` ` `// Only set the first occurrence if ` ` ` `// it hasn't already been set ` ` ` `if` `(first[index] == -` `1` `) ` ` ` `first[index] = i; ` ` ` ` ` `last[index] = i; ` ` ` `} ` ` ` ` ` `// To store the result ` ` ` `int` `ans = -` `1` `, maxVal = -` `1` `; ` ` ` ` ` `// For every alphabet ` ` ` `for` `(` `int` `i = ` `0` `; i < MAX; i++) ` ` ` `{ ` ` ` ` ` `// If current alphabet doesn't appear ` ` ` `// in the given String ` ` ` `if` `(first[i] == -` `1` `) ` ` ` `continue` `; ` ` ` ` ` `// If the current character has ` ` ` `// the highest value so far ` ` ` `if` `((last[i] - first[i]) > maxVal) ` ` ` `{ ` ` ` `maxVal = last[i] - first[i]; ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` ` ` `return` `(` `char` `)(ans + ` `'a'` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"abbba"` `; ` ` ` `int` `len = str.length(); ` ` ` ` ` `System.out.print(maxAlpha(str, len)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python

`# Python implementation of the approach ` `MAX` `=` `26` ` ` `# Function to return the maximum ` `# valued alphabet ` `def` `maxAlpha(` `str` `, ` `len` `): ` ` ` ` ` `# To store the first and the last ` ` ` `# occurrence of all the characters ` ` ` ` ` `# Set the first and the last occurrence ` ` ` `# of all the characters to -1 ` ` ` ` ` `first ` `=` `[` `-` `1` `for` `x ` `in` `range` `(` `MAX` `)] ` ` ` `last ` `=` `[` `-` `1` `for` `x ` `in` `range` `(` `MAX` `)] ` ` ` ` ` `# Update the occurrences of the characters ` ` ` `for` `i ` `in` `range` `(` `0` `,` `len` `): ` ` ` ` ` `index ` `=` `ord` `(` `str` `[i])` `-` `97` ` ` ` ` `# Only set the first occurrence if ` ` ` `# it hasn't already been set ` ` ` `if` `(first[index] ` `=` `=` `-` `1` `): ` ` ` `first[index] ` `=` `i ` ` ` ` ` `last[index] ` `=` `i ` ` ` ` ` `# To store the result ` ` ` `ans ` `=` `-` `1` ` ` `maxVal ` `=` `-` `1` ` ` ` ` `# For every alphabet ` ` ` `for` `i ` `in` `range` `(` `0` `,` `MAX` `): ` ` ` ` ` `# If current alphabet doesn't appear ` ` ` `# in the given string ` ` ` `if` `(first[i] ` `=` `=` `-` `1` `): ` ` ` `continue` ` ` ` ` `# If the current character has ` ` ` `# the highest value so far ` ` ` `if` `((last[i] ` `-` `first[i]) > maxVal): ` ` ` `maxVal ` `=` `last[i] ` `-` `first[i]; ` ` ` `ans ` `=` `i ` ` ` ` ` `return` `chr` `(ans ` `+` `97` `) ` ` ` `# Driver code ` `str` `=` `"abbba"` `len` `=` `len` `(` `str` `) ` ` ` `print` `(maxAlpha(` `str` `, ` `len` `)) ` ` ` `# This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `MAX = 26; ` ` ` `// Function to return the maximum ` `// valued alphabet ` `static` `char` `maxAlpha(String str, ` `int` `len) ` `{ ` ` ` ` ` `// To store the first and the last ` ` ` `// occurrence of all the characters ` ` ` `int` `[]first = ` `new` `int` `[MAX]; ` ` ` `int` `[]last = ` `new` `int` `[MAX]; ` ` ` ` ` `// Set the first and the last occurrence ` ` ` `// of all the characters to -1 ` ` ` `for` `(` `int` `i = 0; i < MAX; i++) ` ` ` `{ ` ` ` `first[i] = -1; ` ` ` `last[i] = -1; ` ` ` `} ` ` ` ` ` `// Update the occurrences of the characters ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `{ ` ` ` ` ` `int` `index = (str[i] - ` `'a'` `); ` ` ` ` ` `// Only set the first occurrence if ` ` ` `// it hasn't already been set ` ` ` `if` `(first[index] == -1) ` ` ` `first[index] = i; ` ` ` ` ` `last[index] = i; ` ` ` `} ` ` ` ` ` `// To store the result ` ` ` `int` `ans = -1, maxVal = -1; ` ` ` ` ` `// For every alphabet ` ` ` `for` `(` `int` `i = 0; i < MAX; i++) ` ` ` `{ ` ` ` ` ` `// If current alphabet doesn't appear ` ` ` `// in the given String ` ` ` `if` `(first[i] == -1) ` ` ` `continue` `; ` ` ` ` ` `// If the current character has ` ` ` `// the highest value so far ` ` ` `if` `((last[i] - first[i]) > maxVal) ` ` ` `{ ` ` ` `maxVal = last[i] - first[i]; ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` ` ` `return` `(` `char` `)(ans + ` `'a'` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String str = ` `"abbba"` `; ` ` ` `int` `len = str.Length; ` ` ` ` ` `Console.Write(maxAlpha(str, len)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

a

**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.

## Recommended Posts:

- 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
- 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
- Count characters at same position as in English alphabet
- Latin alphabet cipher
- Smallest alphabet greater than a given character
- Check if frequency of each character is equal to its position in English Alphabet
- 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
- Lexicographically smaller string by swapping at most one character pair

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.