Watch 9 video solutions for Maximum Value of a String in an Array, a easy level problem involving Array, String. This walkthrough by Developer Docs has 949 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |