1796. Second Largest Digit in a String LeetCode Solution

In this guide, you will get 1796. Second Largest Digit in a String LeetCode Solution with the best time and space complexity. The solution to Second Largest Digit in a String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Second Largest Digit in a String solution in C++
  4. Second Largest Digit in a String solution in Java
  5. Second Largest Digit in a String solution in Python
  6. Additional Resources
1796. Second Largest Digit in a String LeetCode Solution image

Problem Statement of Second Largest Digit in a String

Given an alphanumeric string s, return the second largest numerical digit that appears in s, or -1 if it does not exist.
An alphanumeric string is a string consisting of lowercase English letters and digits.

Example 1:

Input: s = “dfa12321afd”
Output: 2
Explanation: The digits that appear in s are [1, 2, 3]. The second largest digit is 2.

Example 2:

Input: s = “abc1111”
Output: -1
Explanation: The digits that appear in s are [1]. There is no second largest digit.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1796. Second Largest Digit in a String LeetCode Solution in C++

class Solution {
 public:
  int secondHighest(string s) {
    int maxDigit = -1;
    int secondMaxDigit = -1;

    for (const char c : s)
      if (isdigit(c)) {
        const int digit = c - '0';
        if (digit > maxDigit) {
          secondMaxDigit = maxDigit;
          maxDigit = digit;
        } else if (maxDigit > digit && digit > secondMaxDigit) {
          secondMaxDigit = digit;
        }
      }

    return secondMaxDigit;
  }
};
/* code provided by PROGIEZ */

1796. Second Largest Digit in a String LeetCode Solution in Java

class Solution {
  public int secondHighest(String s) {
    int maxDigit = -1;
    int secondMaxDigit = -1;

    for (final char c : s.toCharArray())
      if (Character.isDigit(c)) {
        final int digit = Character.getNumericValue(c);
        if (digit > maxDigit) {
          secondMaxDigit = maxDigit;
          maxDigit = digit;
        } else if (maxDigit > digit && digit > secondMaxDigit) {
          secondMaxDigit = digit;
        }
      }

    return secondMaxDigit;
  }
}
// code provided by PROGIEZ

1796. Second Largest Digit in a String LeetCode Solution in Python

class Solution:
  def secondHighest(self, s: str) -> int:
    maxDigit = -1
    secondMaxDigit = -1

    for c in s:
      if c.isdigit():
        d = int(c)
        if d > maxDigit:
          secondMaxDigit = maxDigit
          maxDigit = d
        elif maxDigit > d > secondMaxDigit:
          secondMaxDigit = d

    return secondMaxDigit
# code by PROGIEZ

Additional Resources

See also  1250. Check If It Is a Good Array LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.