You are given a string num
, representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num
, or an empty string ""
if no odd integer exists.
A substring is a contiguous sequence of characters within a string.
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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find the biggest odd number that can be formed from the ending part of a given number, which is represented as a string. The brute force method will try all possible ending parts of the number, one by one, to see if any of them are odd.
Here's how the algorithm would work step-by-step:
def largest_odd_number_in_string(number):
# Iterate from the full number down to single digits
for substring_length in range(len(number), 0, -1):
possible_odd_number = number[:substring_length]
# Check if the current substring is odd
if int(possible_odd_number) % 2 != 0:
return possible_odd_number
# If no odd number is found, return an empty string
return ""
To find the largest odd number within a string of digits, we want to avoid unnecessary work. The key idea is that an odd number must end in an odd digit, and we want the longest possible odd number, so we start from the end and work backwards.
Here's how the algorithm would work step-by-step:
def largestOddNumber(number):
for index in range(len(number) - 1, -1, -1):
# Start from the end to find the rightmost odd digit.
current_digit = int(number[index])
if current_digit % 2 != 0:
# If odd, return the substring up to and including this digit.
return number[:index+1]
# No odd digit found, so return an empty string.
return ""
Case | How to Handle |
---|---|
Empty input string | Return empty string as specified in the problem when no odd number exists. |
Input string containing only even digits | Return empty string since no odd substring can be formed. |
Input string with a single odd digit | Return the input string itself as it's the largest odd number. |
Very large input string (close to maximum allowed string length) | Iterate from the end of the string; this avoids unnecessary substring creation and scales linearly. |
Input string starts with leading zeros followed by an odd number | The substring starting from the first odd digit will be the largest odd number; leading zeros are irrelevant since we are finding the largest value, not numerical value. |
Input string ends with an odd number | The entire input string is the largest odd number. |
Input string contains mixture of leading/trailing even digits and only one odd number | Iterating from the end and removing even digits ensures finding the largest odd substring quickly. |
Input string consists of only the character '0' | Return an empty string as '0' is not odd. |