Javatpoint Logo
Javatpoint Logo

Find the Maximum English Letter Present in Both Lowercase and Uppercase

Problem Statement:

You are given a string of English letters denoted as s. Your task is to find and return the uppercase English letter that occurs both as a lowercase and uppercase letter in the string. If there is no such letter, return an empty string.

Java Implementation Using HashSet

Output:

"R"
 ""

Problem Explanation:

  • Finding the biggest English letter which has both lowercase and uppercase forms inside a given string s is the problem here. Unique characters need to be identified and then looped through the uppercase letters until the largest one with both cases is determined.

Function Explanation:

  • GreatestLetter function has HashSet that contains distinct characters in the input string. It then cycles throughout capital letters to ascertain their occurrence simultaneously with the preceding lowercase alphabets from the collection. The upper case for the first such pair was found.

Time Complexity:

  • HashSet Population (First Loop): In this case, the first loop goes through each character in the string, resulting in an average runtime of O(N), with N representing the size of the string.
  • Uppercase Letters Check (Second Loop): Secondly, the second loop only passes through a certain group of capital letters, producing a constant time algorithm.
  • The main iteration over the input string has a time complexity of O(N), which, therefore, means an overall time complexity of O(N).

Space Complexity:

  • Primarily, the space complexity depends on the HashSet used for storing unique characters.
  • HashSet Storage:At its extreme, with every character appearing only once as a member of the set, the worst-case space complexity is O ( N).
  • Uppercase Letters:The constant number of uppercase letters in the second loop determines the O(1) space complexity.

Using the Count Array Method

Output:

"R"
""

Explanation:

Function Explanation:

  • The greatest Letter function searches for the biggest English letter that exists in both lower and upper case and is found in the given string 's.' the presence of these letters being uppercase and lowercase is checked using two arrays, 'up' and 'low.' The function processes the string and stores the letters in an array.
  • Then, it goes through all the arrays to find the highest letter that appears in the two possibilities.

Time Complexity:

  • Array Population (First Loop): The first loop loops every character of the input string and checks if it is either an upper- or lowercase character, as well as updating an appropriate array. As a result, this results in O(N) time complexity for a string with a length of N.
  • Array Comparison (Second Loop): In this case, the second loop goes by an array of twenty-six and checks for a letter in either situation. This, hence, gives a continuous time performance.
  • The total complexity time is O(N). As a result, the main linear iteration through the input string is dominating.

Space Complexity:

  • Uppercase and Lowercase Arrays: Two arrays (both up and low) with 26 elements in total represent whether or not a given letter is considered uppercase or lowercase. Hence, each array has a space complexity of O(1), and the space complexity of the entire program is O(1).
  • Other Variables: Other variables, such as ans and loop counters, require constant space and are independent of the size of the input.

Next TopicK-way Merge Sort





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA