Skip to main content

Command Palette

Search for a command to run...

Largest Odd Number in String

Published
1 min read
Largest Odd Number in String
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 1903

class Solution {
    // Method to find the largest odd number by removing trailing even digits
    public String largestOddNumber(String num) {
        // Iterate through the string from right to left
        for(int i = num.length() - 1; i >= 0; i--) {
            char temp = num.charAt(i);
            int intVal = temp - '0'; // Convert the character to its integer value
            // Check if the digit is odd
            if(intVal % 2 != 0) {
                // If the digit is odd, return the substring from the beginning to the current index
                String result = num.substring(0, i + 1);
                return result;
            }
        }  
        // If no odd digits are found, return an empty string
        return "";      
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.