Largest Odd Number in String - Problem
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.
Input & Output
Example 1 — Basic Case with Odd Ending
$
Input:
num = "52301"
›
Output:
"52301"
💡 Note:
The entire string ends with '1' which is odd, so "52301" is the largest odd substring.
Example 2 — Even Ending, Find Rightmost Odd
$
Input:
num = "5234"
›
Output:
"523"
💡 Note:
The string ends with '4' (even), but '3' at position 2 is the rightmost odd digit, so we take "523".
Example 3 — No Odd Digits
$
Input:
num = "246"
›
Output:
""
💡 Note:
All digits are even (2, 4, 6), so no odd substring exists. Return empty string.
Constraints
- 1 ≤ num.length ≤ 105
- num consists of only digits
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string representing a large number
2
Find Strategy
Locate rightmost odd digit for maximum length
3
Extract Result
Take substring from start to that odd digit
Key Takeaway
🎯 Key Insight: The largest odd substring always ends with the rightmost odd digit
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code