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.
If the entire string is considered as a whole number, you only need to check the last digit to determine if it's odd. This is because the entire substring from start to that odd digit will be the largest odd number available. If there is no odd digit, then there's no odd number in the string.
This function iterates from the end of the string, checking each digit to see if it is odd. Once it finds an odd digit, it marks the end of the string there and returns the substring from the start to that point.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1) because only a constant amount of space is used.
By utilizing regular expressions, find the largest substring ending in any odd digit directly. This will require reversing the string and searching for the first odd digit using a pattern.
This solution reverses the string and searches for the first occurrence of an odd digit using a regex pattern. Once found, it calculates the original position and slices the string accordingly.
Python
JavaScript
Time Complexity: O(n), mainly due to the string reversal and regex search.
Space Complexity: O(n) for the reversed string.
We can traverse the string from the end to the beginning, find the first odd number, and then return the substring from the beginning to this odd number. If there is no odd number, return an empty string.
The time complexity is O(n), where n is the length of the string num. Ignoring the space consumption of the answer string, the space complexity is O(1).
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Checking Last Odd Digit | Time Complexity: O(n), where n is the length of the string. |
| Using Regular Expression | Time Complexity: O(n), mainly due to the string reversal and regex search. |
| Reverse Traversal | — |
| 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. |
1. Largest Odd Number in String | Strings - Easy | #LearnDSA | Leetcode 1903 • Ayushi Sharma • 26,525 views views
Watch 9 more video solutions →Practice Largest Odd Number in String with our built-in code editor and test cases.
Practice on FleetCode