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.
For example:
num = "52"
, the output should be "5"
. The only non-empty substrings are "5"
, "2"
, and "52"
. "5"
is the only odd number.num = "4206"
, the output should be ""
. There are no odd numbers in "4206"
.num = "35427"
, the output should be "35427"
. "35427"
is already an odd number.Write a function that takes a string num
as input and returns the largest-valued odd integer substring, or an empty string if none exists. Consider the edge cases such as when the input string contains only even numbers, or when the entire input string is already an odd number. Optimize for time and space complexity.
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:
The brute force way to find the largest odd number in a string is like trying every possible ending. We'll start from the full string and keep chopping off characters from the right until we find an odd number.
Here's how the algorithm would work step-by-step:
def largest_odd_number_in_string_brute_force(number):
string_length = len(number)
# Iterate from the end of the string to the beginning
for i in range(string_length, 0, -1):
substring = number[:i]
last_digit_string = substring[-1]
# Check if the last digit is odd
last_digit = int(last_digit_string)
if last_digit % 2 != 0:
#If it is, return the substring
return substring
# If no odd digit is found, return an empty string
return ""
The goal is to find the largest odd number that's a substring of the given string. Instead of checking all possible substrings, we'll work backwards from the end of the string to find the first odd digit.
Here's how the algorithm would work step-by-step:
def largestOddNumber(number: str) -> str:
for i in range(len(number) - 1, -1, -1):
# Check digits from right to left for oddness.
current_digit = int(number[i])
if current_digit % 2 != 0:
# If odd, return the substring up to and including this digit.
return number[:i+1]
# If no odd digit is found return empty string.
return ""
Case | How to Handle |
---|---|
Null or empty string input | Return an empty string if the input is null or empty since no odd number can exist. |
String contains no odd digits | Iterate from right to left; if no odd digit is found, the loop completes and return empty string. |
String with a single digit, which is odd | Return the single-digit string as it is the largest odd number. |
String with a single digit, which is even | Return an empty string since no odd digit is present. |
String consists only of even digits | Return an empty string because there's no odd number to be extracted. |
String with leading zeros | The algorithm should correctly process leading zeros as part of the string and return the substring up to the last odd digit. |
String with a very long sequence of even digits followed by a single odd digit | The right-to-left iteration will efficiently find the odd digit without iterating through the entire string if possible. |
Maximum length string consisting of odd numbers | Algorithm scales linearly with string length, so should handle maximum-length input without significant performance issues. |