Maximum Value of a String in an Array

Easy
538.

Maximum Value of a String in an Array

Maximum Value of a String in an Array is an easy coding problem on arrays and strings, asked in Amazon and TCS interviews.

The value of an alphanumeric string is defined as follows:

  • If it consists only of digits, its value is that number (leading zeros allowed).
  • Otherwise its value is its length.

Given an array strs, return the maximum value among its strings.

Example 1

Input
strs = ["alic3","bob","3","4","00000"]
Output
5
Explanation
"alic3" has length 5; the numeric strings are worth 3, 4 and 0.

Example 2

Input
strs = ["1","01","001","0001"]
Output
1
Explanation
All are the number 1 after ignoring leading zeros.

Example 3

Input
strs = ["abc","12345"]
Output
12345

Constraints

  • 1 <= strs.length <= 100
  • 1 <= strs[i].length <= 9
  • strs[i] consists of lowercase English letters and digits.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} strs
 * @return {number}
 */
var maximumValue = function(strs) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Maximum Value of a String in an Array — Problem & Solution

Maximum Value of a String in an Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Maximum Value of a String in an Array is an easy coding problem on arrays and strings, asked in Amazon and TCS interviews.

  • Difficulty: Easy
  • Topics: Arrays, Strings
  • Asked at: Amazon, TCS
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

The value of an alphanumeric string is defined as follows:

  • If it consists only of digits, its value is that number (leading zeros allowed).
  • Otherwise its value is its length.

Given an array strs, return the maximum value among its strings.

Example 1

Input: strs = ["alic3","bob","3","4","00000"]
Output: 5
Explanation: "alic3" has length 5; the numeric strings are worth 3, 4 and 0.

Example 2

Input: strs = ["1","01","001","0001"]
Output: 1
Explanation: All are the number 1 after ignoring leading zeros.

Example 3

Input: strs = ["abc","12345"]
Output: 12345

Constraints

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

Hints for Maximum Value of a String in an Array

  1. Hint 1

    Test whether the string is all digits before deciding which rule applies.

  2. Hint 2

    Length is at most 9, so a digit string always fits in a 32-bit integer.

  3. Hint 3

    Track a running maximum.

How to solve Maximum Value of a String in an Array

Each string's value is decided by a single all-digits test, after which the answer is a plain maximum.

Approach

  1. For each string, check whether every character is a digit.
  2. If so, parse it as an integer; otherwise take its length.
  3. Keep the running maximum and return it.

Why it works

The two rules are mutually exclusive and cover every input, so the value function is total; the maximum over independent values is a simple fold.

Complexity

  • Time — O(total characters)
  • Space — O(1)

Pitfalls

  • Parsing without the all-digits test throws (or silently returns 0) on "alic3".
  • Leading zeros are harmless once parsed as a number — "00000" is 0, not 5.
  • With at most 9 digits the maximum is 999,999,999, safely inside a 32-bit int.

Reference solution

Python

from typing import List

def maximumValue(strs: List[str]) -> int:
    best = 0
    for s in strs:
        v = int(s) if s.isdigit() else len(s)
        if v > best:
            best = v
    return best

Java

public static int maximumValue(String[] strs) {
    int best = 0;
    for (String s : strs) {
        boolean allDigits = s.length() > 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c < '0' || c > '9') { allDigits = false; break; }
        }
        int v = allDigits ? Integer.parseInt(s) : s.length();
        if (v > best) best = v;
    }
    return best;
}

C++

int maximumValue(vector<string>& strs) {
    int best = 0;
    for (auto& s : strs) {
        bool allDigits = !s.empty();
        for (char c : s) {
            if (c < '0' || c > '9') { allDigits = false; break; }
        }
        int v = allDigits ? stoi(s) : (int) s.size();
        if (v > best) best = v;
    }
    return best;
}

JavaScript

var maximumValue = function(strs) {
    let best = 0;
    for (let i = 0; i < strs.length; i++) {
        const s = strs[i];
        let allDigits = s.length > 0;
        for (let j = 0; j < s.length; j++) {
            const c = s.charAt(j);
            if (c < "0" || c > "9") { allDigits = false; break; }
        }
        const v = allDigits ? parseInt(s, 10) : s.length;
        if (v > best) best = v;
    }
    return best;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings