Largest Odd Number in String

Updated on 16 June, 2025
Largest Odd Number in String header image

Problem Statement

You are given a string num which represents a large integer. Your task is to find the largest-valued odd integer that can be extracted as a non-empty substring from num. If no such odd integer can be found, your function should return an empty string "". In this context, a substring refers to any contiguous sequence of characters extracted from num. The challenge emphasizes finding the substring that not only represents an odd integer but also is the largest one among all possible odd substrings.

Examples

Example 1

Input:

num = "52"

Output:

"5"

Explanation:

The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.

Example 2

Input:

num = "4206"

Output:

""

Explanation:

There are no odd numbers in "4206".

Example 3

Input:

num = "35427"

Output:

"35427"

Explanation:

"35427" is already an odd number.

Constraints

  • 1 <= num.length <= 105
  • num only consists of digits and does not contain any leading zeros.

Approach and Intuition

To solve this problem, we need a clear understanding of how to determine which integers are odd and how to compare the sizes of these integers when presented as strings. Here's the step-by-step approach:

  1. Loop through the string num from the last character to the first.

    • Since the problem seeks the largest integer and numbers are evaluated based on their highest significant digit first (leftmost digit), starting from the end allows us to find the highest odd digit as soon as possible.
  2. Check each character to determine if it is an odd digit.

    • A digit is odd if, when divided by 2, it leaves a remainder. In terms of digits, these would be 1, 3, 5, 7, and 9.
  3. Once you find the first odd digit (starting from the end of the string), the substring from the start of num to this digit inclusive is your answer.

    • This approach works because, in numeric terms, any leading digits will make the number larger, provided the overall number is odd. Therefore, as soon as we identify the last odd digit, we know that the largest odd number we can form will include all numbers up to this point.
  4. If you traverse the entire string and find no odd digits, return an empty string "".

    • This means that there are no odd integers in any substring of num.

By using this approach:

  • We ensure efficiency, as the string is scanned only once.
  • We find not just any odd number, but the largest possible based on its placement in num.
  • We guarantee that the method will run in linear time relative to the length of num, which is optimal given the constraints.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    string findLargestOdd(string digits) {
        for (int idx = digits.length() - 1; idx >= 0; --idx) {
            if ((digits[idx] - '0') % 2 == 1) {
                return digits.substr(0, idx + 1);
            }
        }
        return "";
    }
};

The provided C++ code defines a class Solution with a method findLargestOdd, which extracts the largest odd-numbered string from a given string of digits. The method iterates backwards from the end of the string until it finds a digit that is an odd number. Once it finds an odd digit, the method returns the substring from the start of the digit string to the position of this odd digit. If no odd digits are found in the entire scan, it returns an empty string.

Here's a break-down of how the code accomplishes this task:

  • Iterates over the string from the last character to the first using a reverse loop.
  • Checks if the current digit is odd by checking (digits[idx] - '0') % 2 == 1.
  • When an odd digit is found, it returns the substring from the beginning to the current digit's position inclusive.
  • If no odd digit is found by the end of the loop, it returns an empty string.

This solution is efficient as it makes a single pass over the input string from the end to the beginning and stops as soon as a valid result is identified.

java
class Solution {
    public String findLargestOdd(String digits) {
        for (int index = digits.length() - 1; index >= 0; index--) {
            if ((digits.charAt(index) - '0') % 2 == 1) {
                return digits.substring(0, index + 1);
            }
        }
        return "";
    }
}

In the "Largest Odd Number in String" problem, the objective is to extract the largest odd-numbered string based on its decimal value from a given string of numerical digits using Java.

Follow these steps in the provided code:

  1. Define a class Solution with a method findLargestOdd, which accepts a String called digits.
  2. Traverse the string from the end to the beginning using a reverse loop, to quickly find the highest odd significant digit.
  3. In each iteration, check if the current digit is odd by using modulo operation (digits.charAt(index) - '0') % 2 == 1.
  4. If an odd digit is found, use substring to return the number from the start of the string to the found odd digit (inclusive).
  5. If no odd digit exists in the entire string, return an empty string.

This approach ensures efficient identification of the largest substring that forms an odd number, leveraging the properties of strings and numerical operations efficiently.

python
class Solution:
    def findLargestOddNumber(self, digits: str) -> str:
        for index in range(len(digits) - 1, -1, -1):
            if int(digits[index]) % 2 == 1:
                return digits[:index + 1]
            
        return ""

The provided Python code defines a function to solve the problem of finding the largest odd-numbered substring from a given string of digits. The function findLargestOddNumber processes the string from right to left, examining each character to determine if it corresponds to an odd digit by checking the modulo operation (% 2 == 1). If an odd digit is found, the function immediately returns the substring from the start of the original string up to and including the current odd digit. If no odd digit is encountered throughout the string, an empty string is returned. This approach ensures that the function efficiently finds and returns the largest odd-numbered substring without unnecessary processing.

Comments

No comments yet.