Leetcode 1796: Second Largest Digit in a String

grid47
grid47
Exploring patterns and algorithms
May 11, 2024 5 min read

You are given an alphanumeric string ’s’, which consists of lowercase English letters and digits. Your task is to find the second largest numerical digit in ’s’. If there is no second largest digit, return -1.
Problem
Approach
Steps
Complexity
Input: You are given an alphanumeric string 's'. This string contains only lowercase letters and digits.
Example: Input: s = 'zab10321xy'
Constraints:
• 1 <= s.length <= 500
• s consists of only lowercase English letters and digits
Output: Return the second largest numerical digit in the string 's', or -1 if it does not exist.
Example: Output: 2
Constraints:
Goal: Find the second largest numerical digit in the given string 's'. If such a digit does not exist, return -1.
Steps:
• Initialize an array to store the two largest digits found.
• Loop through the string and extract all digits.
• For each digit, compare it with the two largest digits found so far and update the array accordingly.
• Return the second largest digit if it exists, otherwise return -1.
Goal: The string contains at least one digit and at most 500 characters.
Steps:
• The string contains only lowercase letters and digits.
• The length of the string is between 1 and 500.
Assumptions:
• The string 's' is always valid and meets the input constraints.
Input: Input: 'ab12cd34'
Explanation: The digits in the string are [1, 2, 3, 4]. The second largest digit is 3.

Input: Input: 'abc1111'
Explanation: The only digit is 1, so there is no second largest digit, and the output is -1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus