Largest odd number in a string

Beginner Problems Basic Strings Easy
  • Fun Fact: A real-world application of such a problem can be found within Big Data Processing software and Financial Technologies (Fintech)
  • In these fields, there’s often a need to deal with large integers, convert them to strings and extract specific parts
  • For instance, certain aspects of financial analysis require the extraction of specific digits or series of digits from large numbers
  • Similarly in network security, similar tasks are required for cryptographic operations to encrypt or decrypt data
  • Thus problems like these underpin several major operations in Fintech and Security industry

Given a string s, representing a large integer, the task is to return the largest-valued odd integer (as a string) that is a substring of the given string s.

The number returned should not have leading zero's. But the given input string may have leading zero.

Examples:

Input : s = "5347"

Output : "5347"

Explanation : The odd numbers formed by given strings are --> 5, 3, 53, 347, 5347.

So the largest among all the possible odd numbers for given string is 5347.

Input : s = "0214638"

Output : "21463"

Explanation : The different odd numbers that can be formed by the given string are --> 1, 3, 21, 63, 463, 1463, 21463.

We cannot include 021463 as the number contains leading zero.

So largest odd number in given string is 21463.

Input : s = "0032579"

Constraints

  • 1 <= s.length <= 103
  • '0' <= s[i] <= '9'

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree