Largest Odd Number in String LeetCode

Key takeaways:

  • A substring is defined as a continuous sequence of characters within the string.

  • If no odd number exists, the output should be an empty string.

  • The algorithm iterates through the string from the end, checking for odd numbers.

  • The substring is returned up to and including the found odd number.

  • If no odd number is found during the iteration, an empty string is returned.

  • Example inputs and outputs include β€œ354” returning β€œ35” and β€œ7634” returning β€œ763.”

  • The time complexity of the algorithm is O(n)O(n), and the space complexity is O(1)O(1).

Finding the Largest Odd Number in a string involves locating the largest odd number within a string that comprises only digits. Solving this LeetCode problem will enhance our skills in string manipulation and algorithm design. For instance, given a string β€œ574839,” the largest odd number that can be formed is β€œ839.” Another example is the string β€œ2468,” where no odd number can be found, resulting in an empty output.

Problem statement

In this problem, a substring is defined as a continuous sequence of characters within a string. Given a string number that represents a number, our task is to return the largest odd substring within this string. If no odd substring is present, we have to return an empty string ("").

Constraints:

  • 1 <= number.length <= 103

  • numberΒ only consists of digits and does not contain any leading zeros.

The table below shows some sample inputs and their expected outputs:

Input

Expected Output

354

35

52

5

7634

763

Knowledge test

Attempt the quiz below to test your concepts on this problem:

1

What will be the result of oddnumber("3246")

A)

6

B)

246

C)

3246

D)

3

Question 1 of 30 attempted

An algorithm to find the largest odd number

Here are the steps for this problem:

  • Iterate through the characters of num, starting from the last character.

  • In each loop, check if it’s an odd number. If it is, return a substring from the beginning (current index i of the loop) up to and including the current character (num[:i + 1]).

  • After iterating through the string, if no odd number was found, return "".

An example of this problem is provided in the slides below:

The string we need to evaluate is 354, and we initialize the value of the largest odd number to an empty string
The string we need to evaluate is 354, and we initialize the value of the largest odd number to an empty string
1 of 4

Code to find the largest odd number

The code solution to this problem is provided below:

def oddnumber(num):
for i in range(len(num) - 1, -1, -1):
if int(num[i]) % 2:
return num[:i + 1]
return ""
num = "354"
result = oddnumber(num)
if result:
print("The largest odd number in the string is", result)
else:
print("No odd numbers are found!")

Code explanation

This code can be explained below:

  • Line 1: Define a function oddnumber, which takes the string num as a parameter.

  • Line 2: Iterate through the string, starting from the last character of the string and progressing backward. We do this as the last character will decide whether the number is odd or even.

  • Line 3: Check if the number is odd.

  • Line 4: If an odd number is found, return the entire string until that index. This is because, regardless of the preceding digits, if the last digit is odd, the entire number is also odd.

  • Lines 7–13: Find the largest odd number in a string by calling the oddnumber function.

You may alter the code at the end to test with your values.

Time complexity

The time complexity of the oddnumber function is O(n)O(n), where nn is the length of the input string num.

Space complexity

Since the space used by the function remains constant regardless of the size of the input string num, the space complexity of the oddnumber function is O(1)O(1).

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to find odd numbers in string?

To find odd numbers in a string, iterate through the characters, check if each character is a digit, convert it to an integer, and verify if it’s odd (i.e., if it is not divisible by 2).


Why is 1 not an odd number?

All natural numbers are testable for oddness except for one. A number must be able to be tested to be classified as odd, and since 1 cannot be tested in this way, it is not considered an odd number. This un-testability makes it impossible to judge whether it is odd or even.


What is the largest odd prime number?

97 is the largest prime number. All prime numbers are odd.


Is 99 the largest 2-digit odd number?

Yes, 99 is the largest 2-digit odd number, as the next odd number, 101, exceeds the two-digit limit


New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
πŸ† Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright Β©2025 Educative, Inc. All rights reserved