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
maxValto 0. - Iterate through each string
sinstrs. - Initialize a boolean flag
isNumericto true. - Iterate through each character
cins. Ifcis not a digit, setisNumericto false and break. - If
isNumericis true, parsesto an integer to getval. Otherwise,valis the length ofs. - Update
maxValwith the maximum ofmaxValandval. - Return
maxVal.
class Solution:
def maximumValue(self, strs: list[str]) -> 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_valComplexity
- 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
maxValto 0. - Iterate through each string
sinstrs. - Check if
smatches the regex pattern^\d+$(start to end digits). - If it matches, parse
sto integer forval. Else,valiss.length. - Update
maxVal. - Return
maxVal.
import re
class Solution:
def maximumValue(self, strs: list[str]) -> 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_valComplexity
- 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
sinstrsto its corresponding value (integer if numeric, else length). - Find the maximum value in the resulting mapped array.
- Return the result.
class Solution:
def maximumValue(self, strs: list[str]) -> 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.