A foundry stamps every cast part with an inspection code — a single line of uppercase and lowercase letters mixed with digits, melted directly into the metal. Quality control wants to know the second-largest distinct digit value that appears anywhere in the code, ignoring the letters entirely and never counting the same digit value more than once even if it is stamped multiple times. If the code contains fewer than two distinct digit values, there is no second-largest digit to report.
A single line containing the inspection code s, made up only of English letters (uppercase and lowercase) and digits.
Print the second-largest distinct digit value appearing in s. If fewer than two distinct digit values appear in s, print -1 instead.
Example 1
Input
AX7B7C3
Expected
3
Explanation
The stamped code is AX7B7C3. Ignoring letters, the digits present are 7, 7, and 3, so the distinct digit values are {7, 3}. The largest is 7 and the second-largest is 3, so the answer is 3.
Example 2
Input
QZ9QZ9
Expected
-1
Explanation
The stamped code is QZ9QZ9. The only digit that appears is 9 (repeated twice), so there is only one distinct digit value. Since fewer than two distinct digits appear, there is no second-largest digit, so the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →