Ctrl K

Second Largest Digit

Easy
You are given a string text made of lowercase English letters and digits. Among the distinct digits that appear in it, find the second largest one and return it as a number. Repeats of the same digit do not count as different digits. If fewer than two distinct digits appear, return -1.
Example 1
text="room404floor7" → 4

The digits present are 0, 4 and 7. The largest is 7 and the next one down is 4.

Example 2
text="agent007" → 0

There are two distinct digits, 0 and 7, so the second largest is 0.

Example 3
text="x9y9z" → -1

Only one distinct digit, 9, appears, so there is no second.

Constraints
  • 1 ≤ len(text) ≤ 500
  • text consists of lowercase English letters and digits
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasySame Letter Cipher EasySame Letters, Reshuffled EasySame When Flipped EasyShuffle Into a Palindrome Easy
loading Python…
Input
text="room404floor7"
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 132 tests on the server, hidden ones included.