All problems
0580EasyMathStringGreedy

Largest Odd Segment of a Serial

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1903Largest Odd Number in String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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