Find the largest Alphabetic character present in the string
Last Updated :
31 Aug, 2023
Given a string str, our task is to find the Largest Alphabetic Character, whose both uppercase and lowercase are present in the string. The uppercase character should be returned. If there is no such character then return -1 otherwise print the uppercase letter of the character.
Examples:
Input: str = “admeDCAB”
Output: D
Explanation:
Both the uppercase and lowercase characters for letter D is present in the string and it is also the largest alphabetical character, hence our output is D.
Input: str = “dAeB”
Output: -1
Explanation:
Although the largest character is d in the string but the uppercase version is not present hence the output is -1.
Naive Approach: To solve the problem mentioned above the naive method is to check for the presence of each character in the string for both uppercase or lowercase character that is for letter A both ‘a’ and ‘A’ should be there in the string. If such a letter is present then will keep track of that character and update only if the current character is greater than the previously chosen character. This method takes O(n2) time in the worst case and can be further optimized.
Efficient Approach: To optimize the above method we use the fact that we have 26 characters in the English alphabet, we can keep track of both lowercase and uppercase character by maintaining an array of size 26. While iterating through the given string we will mark true in both the array if the current character is present in both uppercase and lowercase letter. After marking all characters present in the respective array we will run a loop from 25 to 0 and check if both the array has ‘true’ marked in them. If yes then we print the uppercase letter of that character otherwise return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string largestCharacter(string str)
{
vector< bool > lower(26, false );
vector< bool > upper(26, false );
for ( auto ch : str) {
if ( islower (ch)) {
lower[ch - 'a' ] = true ;
}
else if ( isupper (ch)) {
upper[ch - 'A' ] = true ;
}
}
for ( int i = 25; i >= 0; i--) {
if (lower[i] == true && upper[i] == true ) {
string str = "" ;
str += char (i + 'A' );
return str;
}
}
return "-1" ;
}
int main()
{
string str = "admeDCAB" ;
cout << largestCharacter(str);
return 0;
}
|
Java
public class Main {
public static String largestCharacter(String str)
{
boolean [] uppercase = new boolean [ 26 ];
boolean [] lowercase = new boolean [ 26 ];
char [] arr = str.toCharArray();
for ( char c : arr) {
if (Character.isLowerCase(c))
lowercase = true ;
if (Character.isUpperCase(c))
uppercase = true ;
}
for ( int i = 25 ; i >= 0 ; i--) {
if (uppercase[i] && lowercase[i])
return ( char )(i + 'A' ) + "" ;
}
return "-1" ;
}
public static void main(String[] args)
{
String str = "admeDCAB" ;
System.out.println(largestCharacter(str));
}
}
|
Python3
def largestCharacter( str ):
uppercase = [ False ] * 26
lowercase = [ False ] * 26
arr = list ( str )
for c in arr:
if (c.islower()):
lowercase[ ord (c) - ord ( 'a' )] = True
if (c.isupper()):
uppercase[ ord (c) - ord ( 'A' )] = True
for i in range ( 25 , - 1 , - 1 ):
if (uppercase[i] and lowercase[i]):
return chr (i + ord ( 'A' )) + ""
return "-1"
str = "admeDCAB"
print (largestCharacter( str ))
|
C#
using System;
class GFG{
public static String largestchar(String str)
{
bool [] uppercase = new bool [26];
bool [] lowercase = new bool [26];
char [] arr = str.ToCharArray();
foreach ( char c in arr)
{
if ( char .IsLower(c))
lowercase = true ;
if ( char .IsUpper(c))
uppercase = true ;
}
for ( int i = 25; i >= 0; i--)
{
if (uppercase[i] && lowercase[i])
return ( char )(i + 'A' ) + "" ;
}
return "-1" ;
}
public static void Main(String[] args)
{
String str = "admeDCAB" ;
Console.WriteLine(largestchar(str));
}
}
|
Javascript
<script>
function largestCharacter(str)
{
let uppercase = new Array(26);
uppercase.fill( false );
let lowercase = new Array(26);
lowercase.fill( false );
let arr = str.split( '' );
for (let c = 0; c < arr.length; c++) {
if (arr == arr.toLowerCase())
lowercase[arr.charCodeAt() - 97] = true ;
if (arr == arr.toUpperCase())
uppercase[arr.charCodeAt() - 65] = true ;
}
for (let i = 25; i >= 0; i--) {
if (uppercase[i] && lowercase[i])
return String.fromCharCode(i +
'A' .charCodeAt()) + "" ;
}
return "-1" ;
}
let str = "admeDCAB" ;
document.write(largestCharacter(str));
</script>
|
Output:
D
Time complexity: O(n) where n is length of string.
Space complexity: O(52)
Share your thoughts in the comments
Please Login to comment...