Leetcode 1903: Largest Odd Number in String

grid47
grid47
Exploring patterns and algorithms
Apr 30, 2024 4 min read

You are given a string num, which represents a large integer. Your task is to find the largest odd integer that can be formed from any non-empty substring of num. If no odd integer exists, return an empty string.
Problem
Approach
Steps
Complexity
Input: A string `num` representing a large integer.
Example: num = "54321"
Constraints:
• 1 <= num.length <= 10^5
• num only consists of digits and does not contain any leading zeros.
Output: Return the largest odd integer (as a string) formed from any non-empty substring of `num`, or an empty string if no odd integer exists.
Example: "54321"
Constraints:
• The output is a string that represents the largest odd number, or an empty string if no odd integer is found.
Goal: Find the largest odd integer from any non-empty substring.
Steps:
• Start by iterating from the end of the string `num`.
• For each digit, check if it is odd.
• If an odd digit is found, return the substring from the beginning of `num` to the current index.
Goal: The input is a string consisting only of digits, with no leading zeros, and the length is between 1 and 10^5.
Steps:
• 1 <= num.length <= 10^5
• num contains only digits and no leading zeros.
Assumptions:
• The string `num` is valid and follows the constraints provided.
• The string will always contain digits.
Input: Input: num = "54321"
Explanation: The string '54321' is already an odd number, so it is the largest odd integer possible.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus