189. Largest Odd Number in String

EasyStringString

You are given a string num representing a large integer.

Return the largest-valued odd integer (as a string) that is a non-empty prefix of num, or an empty string '' if no odd integer exists.

An integer is odd if its last digit is odd.

Input: A string num of digit characters.

Output: The longest prefix of num that ends with an odd digit, or '' if none exists.

Examples

Example 1
Input: 52
Output: 5
Explanation: Prefixes: '5'(odd),'52'(even). Largest odd prefix is '5'.
Example 2
Input: 4206
Output: 
Explanation: All prefixes end in even digits → no odd prefix.
Example 3
Input: 35427
Output: 35427
Explanation: '35427' ends in 7 (odd) → whole string is valid.

Constraints

Asked by

AccentureBloombergAmazonMetaGoogleMicrosoft
Solve this problem in the editor →