All problems
0885EasyHash TableString

Runner-Up Digit on a Tag

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1796Second Largest Digit in a String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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