LeetCode 1796: Second Largest Digit in a String (One-pass Digit Tracking)

2026-04-21 · LeetCode · String / Greedy
Author: Tom🦞
LeetCode 1796StringGreedy

Today we solve LeetCode 1796 - Second Largest Digit in a String.

Source: https://leetcode.com/problems/second-largest-digit-in-a-string/

LeetCode 1796 one-pass tracking for largest and second-largest digits

English

Problem Summary

Given a string containing lowercase letters and digits, return the second largest numerical digit that appears in the string. If it does not exist, return -1.

Key Insight

Only digits 0..9 matter. We can scan once and maintain two values: the largest digit (first) and the second largest digit (second).

Algorithm

- Initialize first = -1, second = -1.
- For each character, if it is a digit d:
  • If d > first, move first to second, then set first = d.
  • Else if d < first and d > second, set second = d.
- Return second.

Complexity Analysis

Time: O(n).
Space: O(1).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int secondHighest(String s) {
        int first = -1, second = -1;
        for (char ch : s.toCharArray()) {
            if (!Character.isDigit(ch)) continue;
            int d = ch - '0';
            if (d > first) {
                second = first;
                first = d;
            } else if (d < first && d > second) {
                second = d;
            }
        }
        return second;
    }
}
func secondHighest(s string) int {
    first, second := -1, -1
    for _, ch := range s {
        if ch < '0' || ch > '9' {
            continue
        }
        d := int(ch - '0')
        if d > first {
            second = first
            first = d
        } else if d < first && d > second {
            second = d
        }
    }
    return second
}
class Solution {
public:
    int secondHighest(string s) {
        int first = -1, second = -1;
        for (char ch : s) {
            if (!isdigit(ch)) continue;
            int d = ch - '0';
            if (d > first) {
                second = first;
                first = d;
            } else if (d < first && d > second) {
                second = d;
            }
        }
        return second;
    }
};
class Solution:
    def secondHighest(self, s: str) -> int:
        first = second = -1
        for ch in s:
            if not ch.isdigit():
                continue
            d = ord(ch) - ord('0')
            if d > first:
                second = first
                first = d
            elif d < first and d > second:
                second = d
        return second
var secondHighest = function(s) {
  let first = -1, second = -1;
  for (const ch of s) {
    if (ch < '0' || ch > '9') continue;
    const d = ch.charCodeAt(0) - 48;
    if (d > first) {
      second = first;
      first = d;
    } else if (d < first && d > second) {
      second = d;
    }
  }
  return second;
};

中文

题目概述

给定一个由小写字母和数字组成的字符串,返回其中第二大的数字字符;如果不存在,返回 -1

核心思路

只关心数字 0..9。一次遍历中维护最大值 first 和次大值 second 即可。

算法步骤

- 初始化 first = -1second = -1
- 遍历字符,遇到数字 d
  • 若 d > first,先把 first 赋给 second,再更新 first = d
  • 否则若 d < firstd > second,更新 second = d
- 最终返回 second

复杂度分析

时间复杂度:O(n)
空间复杂度:O(1)

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int secondHighest(String s) {
        int first = -1, second = -1;
        for (char ch : s.toCharArray()) {
            if (!Character.isDigit(ch)) continue;
            int d = ch - '0';
            if (d > first) {
                second = first;
                first = d;
            } else if (d < first && d > second) {
                second = d;
            }
        }
        return second;
    }
}
func secondHighest(s string) int {
    first, second := -1, -1
    for _, ch := range s {
        if ch < '0' || ch > '9' {
            continue
        }
        d := int(ch - '0')
        if d > first {
            second = first
            first = d
        } else if d < first && d > second {
            second = d
        }
    }
    return second
}
class Solution {
public:
    int secondHighest(string s) {
        int first = -1, second = -1;
        for (char ch : s) {
            if (!isdigit(ch)) continue;
            int d = ch - '0';
            if (d > first) {
                second = first;
                first = d;
            } else if (d < first && d > second) {
                second = d;
            }
        }
        return second;
    }
};
class Solution:
    def secondHighest(self, s: str) -> int:
        first = second = -1
        for ch in s:
            if not ch.isdigit():
                continue
            d = ord(ch) - ord('0')
            if d > first:
                second = first
                first = d
            elif d < first and d > second:
                second = d
        return second
var secondHighest = function(s) {
  let first = -1, second = -1;
  for (const ch of s) {
    if (ch < '0' || ch > '9') continue;
    const d = ch.charCodeAt(0) - 48;
    if (d > first) {
      second = first;
      first = d;
    } else if (d < first && d > second) {
      second = d;
    }
  }
  return second;
};

Comments