Strings String basics

125. Largest Odd Number in String

Easy

You are given a string num that holds a large integer, one digit per character. Return the largest odd integer that appears as a substring of num, as a string. If no substring is an odd integer, return the empty string.

A substring is a run of one or more characters that sit next to each other in num. The substrings of "352" are "3", "5", "2", "35", "52" and "352".

Answers are shown in double quotes, so an empty answer shows as "".

Example 1

Input: num = "583914"

Output: "58391"

Explanation: The whole number, 583914, is even. The largest odd number among its substrings is 58391.

Example 2

Input: num = "2460"

Output: ""

Explanation: Every substring is an even number, so the answer is the empty string.

Example 3

Input: num = "10200"

Output: "1"

Explanation: Zero counts as even. The only odd number among the substrings is 1.

Check your understanding

Work out the answer for this input before you write any code.

num = "35246"

Constraints

  • 1 ≤ num.length ≤ 105
  • num holds only the digits 0 to 9.
  • num has no leading zeros: it starts with 0 only when it is "0".

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.