Largest Odd Number in String

You are given a string num representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists. A substring is a contiguous sequence of characters within a string.

30:00

Largest Odd Number in String
easy
Free written solution
Topics
Companies

You are given a string num representing a large integer. Return the largest-valued odd integer (as a string) that is a non-empty substring of num, or an empty string "" if no odd integer exists. A substring is a contiguous sequence of characters within a string.

Example 1:
Input: {"num":"52"}
Output: 5
Constraints:
  • 1≤num.length≤1051 \leq \text{num.length} \leq 10^5

  • num only consists of digits and does not contain any leading zeros.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"num":"52"}

num has 2 digits. We want the largest odd number that appears as a contiguous piece of it.

num

scan right to left ←

5

0

2

1

odd digit

even tail, dropped

kept prefix

dropped: 0

last digit odd? -

Variables
VariableValue
i-1
DepthFunction Call
Stack empty
0/4