Taro Logo

Largest Odd Number in String

Easy
Apple logo
Apple
1 view
Topics:
Strings

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:

  1. If num = "52", the output should be "5". The non-empty substrings are "5", "2", and "52". "5" is the only odd number.
  2. If num = "4206", the output should be "". There are no odd numbers in "4206".
  3. If num = "35427", the output should be "35427". "35427" is already an odd number.

Write an efficient algorithm to solve this problem. What is the time and space complexity of your solution? Can you handle very large numbers efficiently? What edge cases should you consider?

Solution


Let's explore different approaches to solve the problem of finding the largest odd substring.

Naive Approach

We can generate all possible substrings of the given string num and check each substring to see if it represents an odd number. We keep track of the largest odd substring found so far.

Code (Python):

def largestOddSubstring_naive(num):
    largest_odd = ""
    for i in range(len(num)):
        for j in range(i, len(num)):
            sub = num[i:j+1]
            if int(sub) % 2 != 0:
                if len(sub) > len(largest_odd):
                    largest_odd = sub
                elif len(sub) == len(largest_odd) and sub > largest_odd:
                    largest_odd = sub
    return largest_odd

Time Complexity: O(n^3), where n is the length of the input string. Generating all substrings takes O(n^2) time, and checking if each substring is odd and comparing it with the current largest odd substring takes O(n) time in the worst case.

Space Complexity: O(n), where n is the length of the input string, to store the largest odd substring.

Optimal Approach

Instead of generating all substrings, we can iterate through the string from right to left. If we find an odd digit, we can simply return the substring from the beginning of the string up to and including that odd digit. This works because we're looking for the largest odd substring. If we find an odd digit towards the end of the number, any substring ending with that digit will be larger than any substring ending with an odd digit earlier in the number.

Code (Python):

def largestOddSubstring(num):
    for i in range(len(num) - 1, -1, -1):
        if int(num[i]) % 2 != 0:
            return num[:i+1]
    return ""

Time Complexity: O(n), where n is the length of the input string. In the worst case, we iterate through the entire string.

Space Complexity: O(1). We only use a constant amount of extra space.

Edge Cases

  • Empty string: If the input string contains no odd digits, the function should return an empty string, which both solutions handle correctly.
  • String with only even digits: The optimal solution correctly handles this case by returning an empty string after iterating through the entire input.
  • Large input string: The optimal solution is efficient even for large input strings, due to its O(n) time complexity.
  • Input string consists of a single digit: The solutions properly check if the single digit is odd and returns either the digit or an empty string.