Largest Odd Number in a String

Basic String Manipulation DSA practice problem on Onlearn.

Difficulty: easy.

Topics: Largest odd number in a string, Strings, String Manipulation, Loops, Conditional Statements, Basic Arithmetic, character manipulation, traversal, time complexity analysis, string manipulation, Character Properties, Iterative Tree Traversals.

Largest Odd Number in String Problem Statement Given a string num representing a large integer, return the largest odd integer (as a string) that is a non empty substring of num. If no odd integer exists, return an empty string. A substring is a contiguous sequence of characters within a string. Input Specification The input consists of a single string num. Output Specification Return a string representing the largest odd integer found as a substring. Constraints 1 <= num.length <= 10^5 num consists only of digits. num does not contain any leading zeros, except for the zero itself (e.g., "0" is valid, "01" is not). Sample Test Cases Sample 1 Input: num = "52" Output: "5" Explanation: The non empty substrings are "5", "2", "52". The odd substrings are "5". The largest is "5". Sample 2 Input: num = "4206" Output: "" Explanation: No digit in the string is odd, so no odd number can be formed. Sample 3 Input: num = "35427" Output: "35427" Explanation: The substrings ending with an odd digit are "7", "47", "547", "35427". To get the largest value, we need the longest possible substring that is odd. This is num itself, "35427", because its last digit '7' is odd.