Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
The value of an alphanumeric string can be defined as:
10, if it comprises of digits only.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 <= 1001 <= strs[i].length <= 9strs[i] consists of only lowercase English letters and digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["alic3","bob","3","4","00000"]
["1","01","001","0001"]
maximum-subarray)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2496: Maximum Value of a String in an Array
class Solution {
public int maximumValue(String[] strs) {
int ans = 0;
for (var s : strs) {
ans = Math.max(ans, f(s));
}
return ans;
}
private int f(String s) {
int x = 0;
for (int i = 0, n = s.length(); i < n; ++i) {
char c = s.charAt(i);
if (Character.isLetter(c)) {
return n;
}
x = x * 10 + (c - '0');
}
return x;
}
}
// Accepted solution for LeetCode #2496: Maximum Value of a String in an Array
func maximumValue(strs []string) (ans int) {
f := func(s string) (x int) {
for _, c := range s {
if c >= 'a' && c <= 'z' {
return len(s)
}
x = x*10 + int(c-'0')
}
return
}
for _, s := range strs {
if x := f(s); ans < x {
ans = x
}
}
return
}
# Accepted solution for LeetCode #2496: Maximum Value of a String in an Array
class Solution:
def maximumValue(self, strs: List[str]) -> int:
def f(s: str) -> int:
return int(s) if all(c.isdigit() for c in s) else len(s)
return max(f(s) for s in strs)
// Accepted solution for LeetCode #2496: Maximum Value of a String in an Array
impl Solution {
pub fn maximum_value(strs: Vec<String>) -> i32 {
let mut ans = 0;
for s in strs.iter() {
let num = s.parse().unwrap_or(s.len());
ans = ans.max(num);
}
ans as i32
}
}
// Accepted solution for LeetCode #2496: Maximum Value of a String in an Array
function maximumValue(strs: string[]): number {
const f = (s: string) => (Number.isNaN(Number(s)) ? s.length : Number(s));
return Math.max(...strs.map(f));
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.