Second Largest Digit in a String

Easy
1188.

Second Largest Digit in a String

Second Largest Digit in a String is an easy coding problem on strings and hash table, asked in Amazon and Google interviews.

The string s mixes lowercase letters and digits.

Among the distinct digits that appear in s, return the second largest one. If fewer than two distinct digits appear, return -1.

Example 1

Input
s = "kairo2026x9"
Output
6
Explanation
The distinct digits are 0, 2, 6 and 9.

Example 2

Input
s = "codekairo7"
Output
-1
Explanation
Only one distinct digit.

Example 3

Input
s = "a5b3c5"
Output
3

Constraints

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters and digits
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s
 * @return {number}
 */
var secondHighest = function(s) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Second Largest Digit in a String — Problem & Solution

Second Largest Digit in a String solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Second Largest Digit in a String is an easy coding problem on strings and hash table, asked in Amazon and Google interviews.

  • Difficulty: Easy
  • Topics: Strings, Hash Table
  • Asked at: Amazon, Google
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

The string s mixes lowercase letters and digits.

Among the distinct digits that appear in s, return the second largest one. If fewer than two distinct digits appear, return -1.

Example 1

Input: s = "kairo2026x9"
Output: 6
Explanation: The distinct digits are 0, 2, 6 and 9.

Example 2

Input: s = "codekairo7"
Output: -1
Explanation: Only one distinct digit.

Example 3

Input: s = "a5b3c5"
Output: 3

Constraints

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

Hints for Second Largest Digit in a String

  1. Hint 1

    Letters can be ignored entirely.

  2. Hint 2

    Repeated digits count once — you need the two largest different digits.

  3. Hint 3

    Keep first and second (both starting at -1) and update them as you see each digit; a digit equal to first changes nothing.

How to solve Second Largest Digit in a String

Track the largest and second-largest distinct digits in one pass, the same way one finds the top two values of an array, skipping duplicates of the current maximum.

Approach

  1. Set first = second = -1.
  2. For each digit character with value d: if d > first, shift second = first and set first = d.
  3. Otherwise, if second < d < first, set second = d.
  4. Return second.

Why it works

After each step first is the largest digit seen and second the largest digit seen that is strictly smaller than first (or -1). A new maximum demotes the old one to second place; a value strictly between them improves second; anything else changes neither. Using strict comparisons makes duplicates harmless.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • "99" has only one distinct digit — the answer is -1, not 9.
  • When a new maximum arrives, the old maximum must move to second.
  • 0 is a real digit; initialise with -1, not 0.

Reference solution

Python

def secondHighest(s: str) -> int:
    first = second = -1
    for c in s:
        if c.isdigit():
            d = int(c)
            if d > first:
                first, second = d, first
            elif second < d < first:
                second = d
    return second

Java

public static int secondHighest(String s) {
    int first = -1, second = -1;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c < '0' || c > '9') continue;
        int d = c - '0';
        if (d > first) {
            second = first;
            first = d;
        } else if (d < first && d > second) {
            second = d;
        }
    }
    return second;
}

C++

int secondHighest(string s) {
    int first = -1, second = -1;
    for (char c : s) {
        if (c < '0' || c > '9') continue;
        int d = c - '0';
        if (d > first) {
            second = first;
            first = d;
        } else if (d < first && d > second) {
            second = d;
        }
    }
    return second;
}

JavaScript

var secondHighest = function(s) {
    var first = -1, second = -1;
    for (var i = 0; i < s.length; i++) {
        var c = s.charCodeAt(i);
        if (c < 48 || c > 57) continue;
        var d = c - 48;
        if (d > first) {
            second = first;
            first = d;
        } else if (d < first && d > second) {
            second = d;
        }
    }
    return second;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings · Hashing: Hash Maps and Hash Sets