Talentd
FleetCode

1796. Second Largest Digit in a String

Easy55.2% acceptance4 companies

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 <= 500
  • s consists of only lowercase English letters and digits.

Asked at 4 companies

Hints open one at a time

Hint 1
First of all, get the distinct characters since we are only interested in those
Hint 2
Let's note that there might not be any digits.
7:37
Still stuck?Read 4 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def secondHighest(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"dfa12321afd"
Input
"abc1111"
Your code is saved on this device.Solution