Skip to main content

Maximum Value of a String in an Array - Solution & Explanation

EasyArrayString11 min readAsked at: Google
Practice this problem

Problem Statement

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.

 

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 <= 100
  • 1 <= strs[i].length <= 9
  • strs[i] consists of only lowercase English letters and digits.

Approach Overview

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 1: Iterative Classification

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.

Code

Python

Java

C#

C++

C

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Classification with Helper Functions

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.

Code

JavaScript

Python

Complexity

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.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

C#

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative ClassificationO(n * m)O(1)Best general solution; efficient single pass through the array
Classification with Helper FunctionsO(n * m)O(1)Cleaner code structure when separating validation and value logic

Video Solution

Leetcode | 2496. Maximum Value of a String in an Array | Easy | Java Solution • Developer Docs • 1,040 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Value of a String in an Array easy or hard?
Maximum Value of a String in an Array is classified as an Easy problem on LeetCode with an acceptance rate around 74%. The task mainly tests string inspection and basic iteration rather than complex data structures or algorithms.
Maximum Value of a String in an Array Python/Java solution
In Python, iterate through the list and use s.isdigit() to classify strings before applying int(s) or len(s). In Java, loop through the array and check characters using Character.isDigit before calling Integer.parseInt. Both implementations run in O(n * m) time and O(1) space.
How to solve Maximum Value of a String in an Array in O(n)?
You cannot strictly achieve O(n) if digit validation requires scanning characters. The practical optimal solution is O(n * m), where m is the maximum string length. Iterate through the array, check if each string is numeric, compute its value accordingly, and update the running maximum.
What is the best approach for Maximum Value of a String in an Array?
The best approach is a single-pass iterative classification. Scan each string, determine whether all characters are digits, and compute either the parsed integer value or the string length. Track the maximum during iteration. This runs in O(n * m) time where n is the number of strings and m is the maximum string length, with O(1) extra space.
Is Maximum Value of a String in an Array asked at Google/Amazon/Meta?
Problems of this style appear in coding interviews at companies like Amazon and Google as easy screening questions. They test basic string parsing, iteration, and attention to edge cases rather than advanced algorithms.
What data structure is used in Maximum Value of a String in an Array?
The core data structure is a simple array of strings. The algorithm processes the array sequentially and performs character checks on each string. No additional structures like hash maps or stacks are required.
What is the time complexity of Maximum Value of a String in an Array?
The time complexity is O(n * m). The algorithm iterates through n strings, and each string may require scanning up to m characters to verify whether it contains only digits. Space complexity remains O(1) because only a few variables are used to store the current value and maximum.

Ready to solve this problem?

Practice Maximum Value of a String in an Array with our built-in code editor and test cases.

Practice on FleetCode