Largest Odd Number in String
Real-world version: Largest Odd Segment of a SerialThe 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.
def largest_odd_number(num: str) -> str:public String largestOddNumber(String num)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Minimum Number of Pushes to Type Word IReal-world version: Eight-Key Field TerminalEasy
- Sum GameReal-world version: Splitting a Tally FairlyMedium
- Smallest Divisible Digit Product IIReal-world version: Least Serial Whose Digits Carry the FactorHard
- Roman to IntegerReal-world version: Harborside Tally ValueEasy