Given an alphanumeric string s, return the second largest numerical digit that appears in s, or -1 if it does not exist.
An alphanumeric string is a string consisting of lowercase English letters and digits.
Example 1:
Input: s = "dfa12321afd" Output: 2 Explanation: The digits that appear in s are [1, 2, 3]. The second largest digit is 2.
Example 2:
Input: s = "abc1111" Output: -1 Explanation: The digits that appear in s are [1]. There is no second largest digit.
Constraints:
1 <= s.length <= 500s consists of only lowercase English letters and digits.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The plan is to first go through the text and gather all the unique numerical digits we see. Once we have this group of numbers, we can easily identify the largest one, and then find the largest among the remaining numbers.
Here's how the algorithm would work step-by-step:
def second_largest_digit(alphanumeric_string):
# Using a set ensures we only store each digit once, fulfilling the unique digits requirement.
unique_digit_collection = set()
for current_character in alphanumeric_string:
if current_character.isdigit():
unique_digit_collection.add(int(current_character))
# We must have at least two unique digits to be able to find a second largest one.
if len(unique_digit_collection) < 2:
return -1
# Sorting the unique digits in descending order places the second largest at a known index.
sorted_list_of_digits = sorted(list(unique_digit_collection), reverse=True)
return sorted_list_of_digits[1]The most efficient way to solve this is to read through the string just once. While reading, we'll keep track of the largest digit and the second-largest digit we have found so far, updating them as we go.
Here's how the algorithm would work step-by-step:
def second_highest(input_string: str) -> int:
# We use -1 as a starting point because it's smaller than any possible digit.
largest_seen_digit = -1
second_largest_seen_digit = -1
for character in input_string:
if character.isdigit():
current_digit_value = int(character)
# If a new largest digit is found, the previous largest becomes the second largest.
if current_digit_value > largest_seen_digit:
second_largest_seen_digit = largest_seen_digit
largest_seen_digit = current_digit_value
# This case is for a digit that is not the new largest, but could be the second largest.
elif current_digit_value < largest_seen_digit and current_digit_value > second_largest_seen_digit:
second_largest_seen_digit = current_digit_value
return second_largest_seen_digit| Case | How to Handle |
|---|---|
| An input string that contains only letters and no digits, such as 'abcde'. | The solution will find no digits and correctly return -1, the default value for when no second largest digit exists. |
| An input string containing multiple occurrences of only one unique digit, like 'a111b1'. | The algorithm identifies the largest digit but fails to find a second one, properly returning -1. |
| The largest and second-largest digits appear multiple times and out of order, like 'xyz4848'. | The algorithm correctly processes duplicates without altering the already stored largest and second-largest values, ensuring the result is based on unique values. |
| An input string where the second-largest digit is 0, such as 'ab10'. | The algorithm correctly handles 0 as a valid digit and can identify it as the second largest. |
| An input string containing all unique digits from 0 to 9, for instance '9876543210'. | The solution correctly identifies 9 as the largest and 8 as the second largest, regardless of their position in the string. |
| A string with only one character, which could be a letter ('a') or a digit ('7'). | The solution processes the single character and returns -1, as a second-largest digit cannot exist with fewer than two unique digits. |
| An input string at the maximum allowed length of 500 characters. | The single-pass solution with constant extra space scales efficiently and handles the maximum input size without performance issues. |
| An empty string input, which is technically disallowed by the constraints. | While constraints guarantee a non-empty string, a robust solution would handle this by returning -1 immediately. |