LeetCode 2496: Maximum Value of a String in an Array (Length-vs-Number Classification)
LeetCode 2496StringSimulationToday we solve LeetCode 2496 - Maximum Value of a String in an Array.
Source: https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/
English
Problem Summary
Given an array of strings strs, define each string's value as:
- its integer value if all characters are digits;
- otherwise, its length.
Return the maximum value among all strings.
Key Insight
Each string can be processed independently. We only need a helper that determines whether a string is purely numeric. If yes, parse it; if not, use len(s). Track a running maximum.
Brute Force and Limitations
A brute-force mindset might involve generating multiple interpretations, but the rule is deterministic: exactly one of two value definitions applies per string. So one linear scan is enough.
Optimal Algorithm Steps
1) Initialize ans = 0.
2) For each string s, check if every character is a digit.
3) If numeric, convert to integer; otherwise use s.length().
4) Update ans = max(ans, value).
5) Return ans.
Complexity Analysis
Let n be the number of strings and L be total characters across all strings.
Time: O(L).
Space: O(1) extra (excluding input storage).
Common Pitfalls
- Treating strings with letters (like "alic3") as partial numbers.
- Forgetting that leading zeros are valid for numeric parsing (e.g. "0001" -> 1).
- Parsing without first checking digit-only condition.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int maximumValue(String[] strs) {
int ans = 0;
for (String s : strs) {
boolean isNum = true;
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
isNum = false;
break;
}
}
int val = isNum ? Integer.parseInt(s) : s.length();
ans = Math.max(ans, val);
}
return ans;
}
}func maximumValue(strs []string) int {
ans := 0
for _, s := range strs {
isNum := true
for _, ch := range s {
if ch < '0' || ch > '9' {
isNum = false
break
}
}
val := 0
if isNum {
for _, ch := range s {
val = val*10 + int(ch-'0')
}
} else {
val = len(s)
}
if val > ans {
ans = val
}
}
return ans
}class Solution {
public:
int maximumValue(vector<string>& strs) {
int ans = 0;
for (const string& s : strs) {
bool isNum = true;
for (char c : s) {
if (!isdigit(c)) {
isNum = false;
break;
}
}
int val = 0;
if (isNum) {
for (char c : s) val = val * 10 + (c - '0');
} else {
val = (int)s.size();
}
ans = max(ans, val);
}
return ans;
}
};class Solution:
def maximumValue(self, strs: list[str]) -> int:
ans = 0
for s in strs:
if s.isdigit():
val = int(s)
else:
val = len(s)
ans = max(ans, val)
return ansvar maximumValue = function(strs) {
let ans = 0;
for (const s of strs) {
const isNum = /^[0-9]+$/.test(s);
const val = isNum ? Number(s) : s.length;
ans = Math.max(ans, val);
}
return ans;
};中文
题目概述
给定字符串数组 strs,每个字符串的值定义为:
- 若全由数字组成,则其值为对应整数;
- 否则其值为字符串长度。
返回所有字符串值中的最大值。
核心思路
逐个字符串独立处理即可:先判断是否“纯数字”,是就转整数,不是就取长度。全程维护最大值。
暴力解法与不足
如果把每个字符串做复杂尝试会浪费计算;本题规则明确且互斥,线性扫描一次就能完成。
最优算法步骤
1)初始化 ans = 0。
2)遍历每个字符串 s,检查是否全部为数字字符。
3)若是纯数字,转为整数;否则取长度。
4)更新 ans = max(ans, value)。
5)返回 ans。
复杂度分析
设字符串总字符数为 L。
时间复杂度:O(L)。
空间复杂度:O(1)(不计输入)。
常见陷阱
- 含字母的字符串(如 "alic3")不能按数字解析。
- 忽略前导零场景:"0001" 解析后应为 1。
- 未先做纯数字校验就直接解析,容易异常或逻辑错误。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int maximumValue(String[] strs) {
int ans = 0;
for (String s : strs) {
boolean isNum = true;
for (int i = 0; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
isNum = false;
break;
}
}
int val = isNum ? Integer.parseInt(s) : s.length();
ans = Math.max(ans, val);
}
return ans;
}
}func maximumValue(strs []string) int {
ans := 0
for _, s := range strs {
isNum := true
for _, ch := range s {
if ch < '0' || ch > '9' {
isNum = false
break
}
}
val := 0
if isNum {
for _, ch := range s {
val = val*10 + int(ch-'0')
}
} else {
val = len(s)
}
if val > ans {
ans = val
}
}
return ans
}class Solution {
public:
int maximumValue(vector<string>& strs) {
int ans = 0;
for (const string& s : strs) {
bool isNum = true;
for (char c : s) {
if (!isdigit(c)) {
isNum = false;
break;
}
}
int val = 0;
if (isNum) {
for (char c : s) val = val * 10 + (c - '0');
} else {
val = (int)s.size();
}
ans = max(ans, val);
}
return ans;
}
};class Solution:
def maximumValue(self, strs: list[str]) -> int:
ans = 0
for s in strs:
if s.isdigit():
val = int(s)
else:
val = len(s)
ans = max(ans, val)
return ansvar maximumValue = function(strs) {
let ans = 0;
for (const s of strs) {
const isNum = /^[0-9]+$/.test(s);
const val = isNum ? Number(s) : s.length;
ans = Math.max(ans, val);
}
return ans;
};
Comments