Maximum Value of a String in an Array
EasyUpdated: Oct 12, 2025
Practice on:
Problem
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.
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, 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.
Solution
Method 1 - Simple Iteration
Intuition
Iterate through each string, check if it is numeric. If so, convert to integer; otherwise, use its length. Track the maximum.
Approach
-
For each string in the array:
- If all characters are digits, convert to integer value.
- Otherwise, use the string's length.
-
Return the maximum value found.
Code
Python
def maximumValue(strs):
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
C++
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int maximumValue(vector<string>& strs) {
int max_val = 0;
for (auto& s : strs) {
bool is_num = all_of(s.begin(), s.end(), ::isdigit);
int val = is_num ? stoi(s) : s.length();
max_val = max(max_val, val);
}
return max_val;
}
Java
class Solution {
public int maximumValue(String[] strs) {
int maxVal = 0;
for (String s : strs) {
int val;
if (s.matches("\\d+")) {
val = Integer.parseInt(s);
} else {
val = s.length();
}
maxVal = Math.max(maxVal, val);
}
return maxVal;
}
}
Go
import "strconv"
func maximumValue(strs []string) int {
maxVal := 0
for _, s := range strs {
val := 0
if _, err := strconv.Atoi(s); err == nil {
val, _ = strconv.Atoi(s)
} else {
val = len(s)
}
if val > maxVal {
maxVal = val
}
}
return maxVal
}
Kotlin
fun maximumValue(strs: Array<String>): Int {
var maxVal = 0
for (s in strs) {
val valNum = s.toIntOrNull() ?: s.length
maxVal = maxOf(maxVal, valNum)
}
return maxVal
}
Rust
pub fn maximum_value(strs: Vec<String>) -> i32 {
let mut max_val = 0;
for s in strs.iter() {
let val = if s.chars().all(|c| c.is_ascii_digit()) {
s.parse::<i32>().unwrap_or(0)
} else {
s.len() as i32
};
max_val = max_val.max(val);
}
max_val
}
Complexity
- Time:
O(n * m)where n = number of strings, m = max string length - Space:
O(1)
Method 2 - Using Map/Functional
Intuition
Use a functional approach to map each string to its value, then take the max.
Approach
- Map each string to its value (int if numeric, else length).
- Return the max of the mapped values.
Code
Python
def maximumValue(strs):
return max(int(s) if s.isdigit() else len(s) for s in strs)
Complexity
- Time:
O(n * m) - Space:
O(1)