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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the character in first string that is present at minimum index in second string
- Find repeated character present first in a string
- Check whether the Average Character of the String is present or not
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Largest index for each distinct character in given string with frequency K
- Find a string such that every character is lexicographically greater than its immediate next character
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Split numeric, alphabetic and special symbols from a String
- Print all the palindromic permutations of given string in alphabetic order
- Find Kth largest string from the permutations of the string with two characters
- Find the count of substrings in alphabetic order
- Find the last non repeating character in string
- Find k'th character of decrypted string | Set 1
- Find one extra character in a string
- Find the Nth occurrence of a character in the given String
- Find last index of a character in a string
- Find the first repeated character in a string
- Given a string, find its first non-repeating character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.