Show problems as
Classic problem #1903EasyMathStringGreedy

Largest Odd Number in String

Real-world version: Largest Odd Segment of a Serial
Tracked in this browser only
Read Largest Odd Number in String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A part carries a serial num, written as a run of decimal digits.

A segment of the serial is any unbroken stretch of one or more of those digits, read in the order they are printed. Each segment stands for a whole number; a segment that begins with a zero simply stands for a smaller number.

Among the segments whose value is odd, find the one of greatest value and return it as a string, digit for digit as it is printed on the part. When no segment of the serial is odd, return the empty string.

Examples

Example 1

Input
num = "9146"
Output
"91"

The segment "91" stands for the odd number 91. The only segments of this serial standing for a larger number are "914", "9146" and "146", and none of those three is odd.

Example 2

Input
num = "2468"
Output
""

Every digit printed here is even, so every segment ends in an even digit and no segment is odd.

Example 3

Input
num = "8003"
Output
"8003"

The whole serial is itself a segment, it stands for 8003, and 8003 is odd. No segment of this serial stands for a larger number.

Constraints

  • 1 <= num.length <= 10^5
  • num holds decimal digits only.
  • num has no leading zero unless it is the single digit 0.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def largest_odd_number(num: str) -> str:
Java
public String largestOddNumber(String num)