Search⌘ K
AI Features

Solution: Largest Odd Number in String

Explore how to identify and extract the largest odd number substring from a numeric string using a greedy algorithm. This lesson guides you through scanning the string from the end to locate the rightmost odd digit, enabling efficient substring extraction with O(n) time and O(1) space complexity.

Statement

You are given a string, num, which represents a large integer. Your task is to find the largest odd-valued integer that can be formed as a non-empty substring of num. Return this odd integer as a string. If no odd integer exists, return an empty string ("").

Note: A substring is a continuous sequence of characters within a string.

Constraints:

  • 11 \leq num.length 104\leq 10^4 ...