Back to blog
Apr 07, 2024
4 min read

Maximum Value of a String in an Array

Find the maximum value in an array where numeric strings represent their integer value and non-numeric strings represent their length.

Difficulty: Easy | Acceptance: 74.20% | Paid: No Topics: Array, String

The value of an alphanumeric string can be defined as:

The numeric representation of the string in base 10, if the string consists only of digits (leading zeros are allowed). The length of the string, otherwise. Given an array strs of alphanumeric strings, return the maximum value of any string in strs.

Examples

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 5.
- "bob" consists only of letters, so its value is its length 3.
- "3" consists only of digits, so its value is its numeric value 3.
- "4" consists only of digits, so its value is its numeric value 4.
- "00000" consists only of digits, so its value is its numeric value 0.
The maximum value is 5, which is the value of "alic3".

Example 2

Input: strs = ["1","01","001","0001"]
Output: 1
Explanation: 
Each string consists only of digits, so their values are their numeric values 1, 1, 1, and 1 respectively.
The maximum value is 1.

Constraints

1 <= strs.length <= 100
2 <= strs[i].length <= 100
strs[i] consists of lowercase English letters and digits.

Approach 1: Iteration with Character Check

Intuition We iterate through each string in the array. For each string, we check if every character is a digit. If it is, we convert the string to an integer; otherwise, we use the string’s length. We keep track of the maximum value encountered.

Steps

  • Initialize maxVal to 0.
  • Iterate through each string s in strs.
  • Initialize a boolean flag isNumeric to true.
  • Iterate through each character c in s. If c is not a digit, set isNumeric to false and break.
  • If isNumeric is true, parse s to an integer to get val. Otherwise, val is the length of s.
  • Update maxVal with the maximum of maxVal and val.
  • Return maxVal.
python
class Solution:
    def maximumValue(self, strs: list[str]) -&gt; int:
        max_val = 0
        for s in strs:
            if s.isdigit():
                val = int(s)
            else:
                val = len(s)
            max_val = max(max_val, val)
        return max_val

Complexity

  • Time: O(N * L), where N is the number of strings and L is the maximum length of a string.
  • Space: O(1), excluding the input storage.
  • Notes: This is the most fundamental approach, checking character by character.

Approach 2: Regular Expression

Intuition We can use a regular expression to determine if a string consists solely of digits. This abstracts the character checking logic into a concise pattern match.

Steps

  • Initialize maxVal to 0.
  • Iterate through each string s in strs.
  • Check if s matches the regex pattern ^\d+$ (start to end digits).
  • If it matches, parse s to integer for val. Else, val is s.length.
  • Update maxVal.
  • Return maxVal.
python
import re

class Solution:
    def maximumValue(self, strs: list[str]) -&gt; int:
        max_val = 0
        for s in strs:
            if re.match(r'^\d+$', s):
                val = int(s)
            else:
                val = len(s)
            max_val = max(max_val, val)
        return max_val

Complexity

  • Time: O(N * L), regex matching typically scans the string.
  • Space: O(1), though regex engines may use auxiliary space.
  • Notes: Cleaner syntax, but regex can have overhead compared to simple character checks.

Approach 3: Functional / One-Liner

Intuition We utilize functional programming constructs like map, reduce, or max to transform the array of strings into an array of values and find the maximum in a declarative way.

Steps

  • Map each string s in strs to its corresponding value (integer if numeric, else length).
  • Find the maximum value in the resulting mapped array.
  • Return the result.
python
class Solution:
    def maximumValue(self, strs: list[str]) -&gt; int:
        return max(int(s) if s.isdigit() else len(s) for s in strs)

Complexity

  • Time: O(N * L), as we must process every character to determine the value.
  • Space: O(N) for the mapped array in languages like JavaScript/Python (unless using a generator), or O(1) for streams in Java.
  • Notes: Concise and readable, but may hide the O(N) space complexity in languages that create intermediate arrays.