You are given a string containing a mix of lowercase English letters and numeric digits. Your task is to identify all unique numeric characters (0-9) that appear in the string and determine the second-highest digit among them.
If the string contains fewer than two distinct numeric digits, return -1.
Example 1:
Input: s = "tech9quiz8" Output: 8 Explanation: The distinct digits present are 9 and 8. The second largest is 8.
Example 2:
Input: s = "x7y7z7" Output: -1 Explanation: Only one distinct digit (7) appears in the string, so we return -1.
Example 3:
Input: s = "coding4fun2day3" Output: 3 Explanation: The distinct digits are 4, 2, and 3. Sorted in descending order: [4, 3, 2]. The second largest is 3.
Example 4:
Input: s = "nodigitshere" Output: -1 Explanation: No numeric characters exist in the string.
Hint 1: Filtering Characters Consider iterating through the string character by character. You can use the
isdigit()method or check if a character falls within the '0' to '9' range to identify numeric characters.
Hint 2: Tracking Unique Values Use a data structure like a set to automatically handle uniqueness of digits as you encounter them. This eliminates the need to manually check for duplicates.
Hint 3: Finding the Second Largest Once you have all unique digits, you can convert them to a sorted list. If the list has at least 2 elements, the second element from the end (or second element when sorted in descending order) is your answer.
Full Solution ` def findSecondLargestDigit(s: str) -> int: # Use a set to track unique digits found in the string unique_digits = set()
# Iterate through each character in the string for char in s: # Check if the character is a digit if char.isdigit(): # Add the digit (as an integer) to our set unique_digits.add(int(char)) # If we have fewer than 2 unique digits, return -1 if len(unique_digits) < 2: return -1 # Sort the unique digits in descending order sorted_digits = sorted(unique_digits, reverse=True) # Return the second largest digit return sorted_digits[1]`
Explanation:
The solution works in three main steps:
- Extract unique digits: We iterate through each character in the string and use the
isdigit()method to identify numeric characters. We store these as integers in a set, which automatically handles uniqueness.- Validate count: Before proceeding, we check if we have at least two distinct digits. If not, we immediately return -1.
- Find second largest: We sort the unique digits in descending order and return the element at index 1 (the second element), which is the second-largest digit.
Time Complexity: O(n + k log k) where n is the length of the string and k is the number of unique digits (at most 10). The iteration takes O(n) and sorting takes O(k log k), but since k ≤ 10, this is effectively O(n).
Space Complexity: O(k) where k is the number of unique digits (at most 10), so effectively O(1) constant space.
Alternative approach: Instead of sorting, you could also iterate through the sorted set twice to find the maximum, remove it, then find the maximum of what remains. However, since we're dealing with at most 10 digits, sorting is simple and efficient.