Watch 10 video solutions for Largest Odd Number in String, a easy level problem involving Math, String, Greedy. This walkthrough by Ayushi Sharma has 26,525 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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" Explanation: The only non-empty substrings are "5", "2", and "52". "5" is the only odd number.
Example 2:
Input: num = "4206" Output: "" Explanation: There are no odd numbers in "4206".
Example 3:
Input: num = "35427" Output: "35427" Explanation: "35427" is already an odd number.
Constraints:
1 <= num.length <= 105num only consists of digits and does not contain any leading zeros.Problem Overview: You are given a numeric string num. The task is to return the largest-valued odd number that can be formed as a substring. Since substrings keep the original order of digits, the largest valid odd number must be a prefix that ends at the rightmost odd digit.
Approach 1: Checking Last Odd Digit (Greedy) (Time: O(n), Space: O(1))
This approach relies on a simple observation: an integer is odd if its last digit is odd (1, 3, 5, 7, 9). To maximize the value of the substring, you want the longest prefix that ends with an odd digit. Iterate from the end of the string toward the beginning and check each digit. As soon as you find an odd digit, return the substring from index 0 to that position inclusive. The scan takes at most n steps and no extra data structures are required, making it an efficient greedy strategy working directly on the string.
Approach 2: Using Regular Expression (Time: O(n), Space: O(n))
Regular expressions can also identify the longest prefix that ends with an odd digit. A pattern such as ^\d*[13579] matches any sequence of digits starting from the beginning of the string and ending with an odd digit. The regex engine scans the string until it finds the last valid odd-ending prefix. While the theoretical time complexity is still O(n), regex introduces additional overhead and temporary allocations compared with direct iteration. This approach is convenient in scripting languages like Python or JavaScript but less common in interviews where explicit math checks on digits are clearer.
Recommended for interviews: The greedy scan from the end is the expected solution. It demonstrates that you recognize the defining property of odd numbers and avoid unnecessary substring generation. Interviewers typically look for this observation and an O(n) pass with constant space. Regex solutions are concise but hide the underlying reasoning and are rarely preferred during whiteboard discussions.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Checking Last Odd Digit (Greedy) | O(n) | O(1) | Best general solution. Single scan from the end with minimal memory. |
| Regular Expression Match | O(n) | O(n) | Useful in scripting languages when concise pattern matching is preferred. |