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.
Input: str = “admeDCAB”
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”
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:
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.
- Print the string by ignoring alternate occurrences of any character
- Find the first non-repeating character from a stream of characters
- Find length of the largest region in Boolean Matrix
- Queue based approach for first non-repeating character in a stream
- Largest Sum Contiguous Subarray
- k largest(or smallest) elements in an array | added Min Heap method
- Largest Rectangular Area in a Histogram | Set 2
- Largest Rectangular Area in a Histogram | Set 1
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- K'th Smallest/Largest Element in Unsorted Array | Set 3 (Worst Case Linear Time)
- Make largest palindrome by changing at most K-digits
- Find if a string is interleaved of two other strings | DP-33
- Reverse words in a given string
- Smallest window that contains all characters of string itself
- URLify a given string (Replace spaces is %20)
- Evaluate a boolean expression represented as string
- Concatenated string with uncommon characters of two strings
- Average of ASCII values of characters of a given string
- Find the middle of a given linked list in C and Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Amal Kumar Choubey