Show problems as
Classic problem #1796EasyHash TableString

Second Largest Digit in a String

Real-world version: Runner-Up Digit on a Tag
Tracked in this browser only
Read Second Largest Digit in a String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tag reads s, a mix of lowercase letters and digits.

Return the second largest digit appearing on the tag, counting each distinct digit once however often it appears. Return -1 when the tag has fewer than two distinct digits.

Examples

Example 1

Input
s = "crate84bin37"
Output
7

The digits on the tag are 8, 4, 3 and 7. The largest is 8, so the runner-up is 7.

Example 2

Input
s = "zzz5zzz5zzz2"
Output
2

Only two distinct digits appear, 5 and 2, however often each is repeated, so the runner-up is 2.

Example 3

Input
s = "shelf7"
Output
-1

The tag holds a single distinct digit, so there is no runner-up.

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters and digits only

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def second_highest(s: str) -> int:
Java
public int secondHighest(String s)