Find the most valued alphabet in the String
Last Updated :
28 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 26;
char maxAlpha(string str, int len)
{
int first[MAX], last[MAX];
for ( int i = 0; i < MAX; i++) {
first[i] = -1;
last[i] = -1;
}
for ( int i = 0; i < len; i++) {
int index = (str[i] - 'a' );
if (first[index] == -1)
first[index] = i;
last[index] = i;
}
int ans = -1, maxVal = -1;
for ( int i = 0; i < MAX; i++) {
if (first[i] == -1)
continue ;
if ((last[i] - first[i]) > maxVal) {
maxVal = last[i] - first[i];
ans = i;
}
}
return ( char )(ans + 'a' );
}
int main()
{
string str = "abbba" ;
int len = str.length();
cout << maxAlpha(str, len);
return 0;
}
|
Java
class GFG
{
static int MAX = 26 ;
static char maxAlpha(String str, int len)
{
int []first = new int [MAX];
int []last = new int [MAX];
for ( int i = 0 ; i < MAX; i++)
{
first[i] = - 1 ;
last[i] = - 1 ;
}
for ( int i = 0 ; i < len; i++)
{
int index = (str.charAt(i) - 'a' );
if (first[index] == - 1 )
first[index] = i;
last[index] = i;
}
int ans = - 1 , maxVal = - 1 ;
for ( int i = 0 ; i < MAX; i++)
{
if (first[i] == - 1 )
continue ;
if ((last[i] - first[i]) > maxVal)
{
maxVal = last[i] - first[i];
ans = i;
}
}
return ( char )(ans + 'a' );
}
public static void main(String[] args)
{
String str = "abbba" ;
int len = str.length();
System.out.print(maxAlpha(str, len));
}
}
|
Python
MAX = 26
def maxAlpha( str , len ):
first = [ - 1 for x in range ( MAX )]
last = [ - 1 for x in range ( MAX )]
for i in range ( 0 , len ):
index = ord ( str [i]) - 97
if (first[index] = = - 1 ):
first[index] = i
last[index] = i
ans = - 1
maxVal = - 1
for i in range ( 0 , MAX ):
if (first[i] = = - 1 ):
continue
if ((last[i] - first[i]) > maxVal):
maxVal = last[i] - first[i];
ans = i
return chr (ans + 97 )
str = "abbba"
len = len ( str )
print (maxAlpha( str , len ))
|
C#
using System;
class GFG
{
static int MAX = 26;
static char maxAlpha(String str, int len)
{
int []first = new int [MAX];
int []last = new int [MAX];
for ( int i = 0; i < MAX; i++)
{
first[i] = -1;
last[i] = -1;
}
for ( int i = 0; i < len; i++)
{
int index = (str[i] - 'a' );
if (first[index] == -1)
first[index] = i;
last[index] = i;
}
int ans = -1, maxVal = -1;
for ( int i = 0; i < MAX; i++)
{
if (first[i] == -1)
continue ;
if ((last[i] - first[i]) > maxVal)
{
maxVal = last[i] - first[i];
ans = i;
}
}
return ( char )(ans + 'a' );
}
public static void Main(String[] args)
{
String str = "abbba" ;
int len = str.Length;
Console.Write(maxAlpha(str, len));
}
}
|
Javascript
<script>
const MAX = 26;
function maxAlpha(str, len)
{
var first = new Array(MAX);
var last = new Array(MAX);
for ( var i = 0; i < MAX; i++) {
first[i] = -1;
last[i] = -1;
}
for ( var i = 0; i < len; i++) {
var index = str[i].charCodeAt(0) -
"a" .charCodeAt(0);
if (first[index] === -1) first[index] = i;
last[index] = i;
}
var ans = -1,
maxVal = -1;
for ( var i = 0; i < MAX; i++) {
if (first[i] === -1) continue ;
if (last[i] - first[i] > maxVal) {
maxVal = last[i] - first[i];
ans = i;
}
}
return String.fromCharCode(ans + "a" .charCodeAt(0));
}
var str = "abbba" ;
var len = str.length;
document.write(maxAlpha(str, len));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...