Skip to main content

Maximum Value of a String in an Array - Video Solutions

EasyArrayString

Leetcode | 2496. Maximum Value of a String in an Array | Easy | Java Solution

Developer Docs
6:411,040 views
10 video solutions available

Maximum Value of a String in an Array - Video Solution

Watch 10 video solutions for Maximum Value of a String in an Array, a easy level problem involving Array, String. This walkthrough by Developer Docs has 1,040 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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.
Read full problem with examples

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.

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