Skip to main content

Command Palette

Search for a command to run...

Maximum Value of a String in an Array

Published
2 min read
Maximum Value of a String in an Array
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.

The value of an alphanumeric string can be defined as:

  • The numeric representation of the string in base 10, if it comprises of digits only.

  • The length of the string, otherwise.

Given an array strs of alphanumeric strings, return the maximum value of any string in strs.

LeetCode Problem - 2496

class Solution {
    public int maximumValue(String[] strs) {
        // Initialize the result to the smallest possible integer value
        int result = Integer.MIN_VALUE;

        // Iterate through each string in the input array
        for (int i = 0; i < strs.length; i++) {
            String currStr = strs[i]; // Current string from the array

            // Check if the current string consists only of digits (numeric string)
            boolean hasNumeric = currStr.matches("\\d+");

            // If the string is numeric, convert it to an integer and compare with the result
            if (hasNumeric) {
                int base10Val = base10(currStr); // Convert the numeric string to an integer
                if (base10Val > result) {
                    result = base10Val; // Update result if the numeric value is larger
                }
            } else {
                // If the string is not numeric, use the string's length for comparison
                int currStrLen = currStr.length();
                if (currStrLen > result) {
                    result = currStrLen; // Update result if the string's length is larger
                }
            }
        }

        // Return the maximum value found (either a numeric value or the length of the longest string)
        return result;
    }

    // Helper method to convert a numeric string to an integer in base 10
    public int base10(String str) {
        return Integer.parseInt(str, 10);
    }
}

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.

Maximum Value of a String in an Array