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.
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"
To determine the largest substring ending with an odd digit, start by iterating backward from the end of the number. This approach efficiently finds the rightmost odd digit by examining each character in reverse order. Once an odd digit is encountered, the substring from the beginning of the number up to and including this digit represents the largest possible odd-ending substring. This process leverages the fact that finding the last occurrence of an odd digit directly provides the longest valid substring.
1. Start by iterating through the string from the end towards the beginning to find the first odd digit. This digit marks the potential end of the largest odd number substring.
2. Once an odd digit is found, skip any leading zeroes from the beginning of the string up to this odd digit.
3. Extract and return the substring starting after the leading zeroes and ending at the identified odd digit. This substring represents the largest odd integer without leading zeroes.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to find the largest odd number
that is a substring of given string */
string largeOddNum(string& s) {
int ind = -1;
// Iterate through the string from the end to beginning
int i;
for (i = s.length() - 1; i >= 0; i--) {
// Break if an odd digit is found
if ((s[i] - '0') % 2 == 1) {
ind = i;
break;
}
}
// Skipping any leading zeroes
i = 0;
while(i <= ind && s[i] == '0') i++;
// Return the largest odd number substring
return s.substr(i, ind - i + 1);
}
};
int main() {
Solution solution;
string num = "504";
string result = solution.largeOddNum(num);
cout << "Largest odd number: " << result << endl;
return 0;
}
import java.util.*;
class Solution {
/* Function to find the largest odd number
that is a substring of given string */
public String largeOddNum(String s) {
int ind = -1;
// Iterate through the string from the end to beginning
int i;
for (i = s.length() - 1; i >= 0; i--) {
// Break if an odd digit is found
if ((s.charAt(i) - '0') % 2 == 1) {
ind = i;
break;
}
}
// If no odd number was found, return an empty string
if (ind == -1) return "";
// Skipping any leading zeroes
i = 0;
while(i <= ind && s.charAt(i) == '0') i++;
// Return the largest odd number substring
return s.substring(i, ind + 1);
}
public static void main(String[] args) {
Solution solution = new Solution();
String num = "504";
String result = solution.largeOddNum(num);
System.out.println("Largest odd number: " + result);
}
}
class Solution:
# Function to find the largest odd number
# that is a substring of given string
def largeOddNum(self, s: str) -> str:
ind = -1
# Iterate through the string from the end to beginning
i = 0
for i in range(len(s) - 1, -1, -1):
# Break if an odd digit is found
if (int(s[i]) % 2) == 1:
ind = i
break
# Skipping any leading zeroes
i = 0
while i <= ind and s[i] == '0':
i += 1
# Return the largest odd number substring
return s[i:ind + 1]
# Driver code
solution = Solution()
num = "504"
result = solution.largeOddNum(num)
print("Largest odd number:", result)
class Solution {
/* Function to find the largest odd number
that is a substring of given string */
largeOddNum(s) {
let ind = -1;
// Iterate through the string from the end to beginning
let i;
for (i = s.length - 1; i >= 0; i--) {
// Break if an odd digit is found
if ((s[i] - '0') % 2 === 1) {
ind = i;
break;
}
}
// Skipping any leading zeroes
i = 0;
while (i <= ind && s[i] === '0') i++;
// Return the largest odd number substring
return s.substring(i, ind + 1);
}
}
// Driver code
const solution = new Solution();
const num = "504";
const result = solution.largeOddNum(num);
console.log("Largest odd number:", result);
Time Complexity O(N): The loop runs once through the string of length N.
Space Complexity O(1): No extra data structures are used only a few extra variables are used which does not affect the complexity as much.