← DiffPush

Largest odd number in string

Baseline Strings · Easy O(n) · O(1)

The Last Odd Checkpoint Decides the Load

A freight scanner reads a continuous stream of digits far too long to fit into any integer register, so values live as strings. The odd-lane loader only accepts containers whose numeric reading is odd, and the dispatcher wants the single largest odd value hiding anywhere in the scanned stream. If the stream contains no odd digit at all, the lane stays empty and the answer is an empty string.

Input: A string num of digits representing a very large non-negative integer without leading zeros.

Output: The largest-valued odd integer that appears as a non-empty contiguous substring of num, returned as a string, or "" when none exists.

Constraints

Examples

Example 1

Input: {"num":"35427"}
Output: "35427"
The final digit 7 is odd, so the entire reading — the longest prefix ending at that digit — is the largest odd value available.

Example 2

Input: {"num":"52"}
Output: "5"
The stream ends in an even digit, so the best odd value is the prefix cut just before it, keeping the leading 5.

Solve this in your browser →

Also on LeetCode ↗