The value of an alphanumeric string can be defined as:
10, if it comprises of digits only.Given an array strs of alphanumeric strings, return the maximum value of any string in strs.
Example 1:
Input: strs = ["alic3","bob","3","4","00000"] Output: 5 Explanation: - "alic3" consists of both letters and digits, so its value is its length, i.e. 5. - "bob" consists only of letters, so its value is also its length, i.e. 3. - "3" consists only of digits, so its value is its numeric equivalent, i.e. 3. - "4" also consists only of digits, so its value is 4. - "00000" consists only of digits, so its value is 0. Hence, the maximum value is 5, of "alic3".
Example 2:
Input: strs = ["1","01","001","0001"] Output: 1 Explanation: Each string in the array has value 1. Hence, we return 1.
Constraints:
1 <= strs.length <= 1001 <= strs[i].length <= 9strs[i] consists of only lowercase English letters and digits.Problem Overview: You receive an array of strings. Each string represents a value using two different rules: if the string contains only digits, its value is the integer it represents; otherwise its value is simply its length. Your task is to compute the maximum value across all strings.
The challenge is classification. For every string, you must determine whether it is numeric or alphanumeric. Once classified, compute either the integer value or the length, then keep track of the maximum. The problem mainly tests careful string inspection and efficient iteration through an array.
Approach 1: Iterative Classification (O(n * m) time, O(1) space)
Iterate through the array once and classify each string character-by-character. For each string, scan all characters and check whether every character falls between '0' and '9'. If a non-digit appears, the string is treated as a regular word and its value becomes len(s). If every character is a digit, convert it to an integer using stoi, parseInt, or equivalent.
Track the maximum value while iterating. The algorithm performs a single pass over the array and a scan over each string. If n is the number of strings and m is the maximum string length, the complexity becomes O(n * m). No additional data structures are required, so the space complexity stays O(1). This approach is straightforward, language‑agnostic, and the most common solution used in interviews.
Approach 2: Classification with Helper Functions (O(n * m) time, O(1) space)
This variation extracts the classification logic into helper functions. One helper checks whether a string is numeric using built-in checks like isdigit(), regular expressions, or a small digit-validation loop. Another helper computes the value: return the parsed integer if numeric, otherwise return the string length.
The main loop simply calls the helper for each string and updates the maximum value. Complexity remains O(n * m) because every character may still be inspected during validation. Space usage stays O(1) since no extra structures are allocated beyond a few variables.
The benefit is readability. Separating classification logic keeps the main loop clean and easier to maintain, especially in languages like JavaScript or Python where helper functions are lightweight.
Recommended for interviews: The iterative classification approach is typically expected. It demonstrates that you can reason about character ranges, iterate efficiently, and maintain running results. The helper-function version is also valid and slightly cleaner in production-style code, but interviewers mainly care that you correctly classify numeric strings and achieve the linear O(n * m) solution.
In this approach, we iterate over each string in the array and classify it as either purely numeric or alphanumeric. For numeric strings, we convert them to an integer value using base 10, and for alphanumeric strings, we use their length as the value. We keep track of the maximum value encountered during this iteration.
This Python function iterates over each string in the given list. It checks if a string contains only digits using the method isdigit(). If it does, the string is converted to an integer. Otherwise, the length of the string is calculated and used as its value. The function keeps track of the maximum value encountered and returns it.
The time complexity of this solution is O(n), where n is the total number of characters across all strings, and the space complexity is O(1) since we are only using a fixed amount of space for tracking the maximum value.
This approach uses helper functions to separately determine if the string is numeric and to calculate the appropriate value. This modular design enhances readability and reusability of the code. We then find the maximum value using these helper functions.
This JavaScript solution uses isNumeric helper function to check if a string is numeric and getStringValue to determine its value. The core function maximumValue applies getStringValue to each string and finds the maximum value using the spread syntax with Math.max.
JavaScript
Python
The time complexity of this method is O(n), where n is the sum of characters in all strings. The space complexity is O(1) as additional space is not required beyond fixed auxiliary variables.
| Approach | Complexity |
|---|---|
| Iterative Classification | The time complexity of this solution is O(n), where n is the total number of characters across all strings, and the space complexity is O(1) since we are only using a fixed amount of space for tracking the maximum value. |
| Classification with Helper Functions | The time complexity of this method is O(n), where n is the sum of characters in all strings. The space complexity is O(1) as additional space is not required beyond fixed auxiliary variables. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Classification | O(n * m) | O(1) | Best general solution; efficient single pass through the array |
| Classification with Helper Functions | O(n * m) | O(1) | Cleaner code structure when separating validation and value logic |
Leetcode | 2496. Maximum Value of a String in an Array | Easy | Java Solution • Developer Docs • 949 views views
Watch 8 more video solutions →Practice Maximum Value of a String in an Array with our built-in code editor and test cases.
Practice on FleetCode