Find the largest Alphabetic character present in the string

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Find the Largest Alphabetic
// Character present in the string of both 
// uppercase and lowercase English characters
  
public class Main {
  
    // Function to find the Largest Alphabetic Character
    public static String largestCharacter(String str)
    {
        // Array for keeping track of both uppercase
        // and lowercase english alphabets
        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;
        }
  
        // Iterate from right side of array
        // to get the largest index character
        for (int i = 25; i >= 0; i--) {
  
            // Check for the character if both its 
            // uppercase and lowercase exist or not
            if (uppercase[i] && lowercase[i])
                return (char)(i + 'A') + "";
        }
  
        // Return -1 if no such character whose 
        // uppercase and lowercase present in 
        // string str
        return "-1";
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str = "admeDCAB";
  
        System.out.println(largestCharacter(str));
    }
}

chevron_right


Output:

D

Time complexity: O(n) where n is length of string.
Space complexity: O(52)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.