Maximum Value of a String in an Array — LeetCode 2496 Python Solution

EasyArrayString
Problem
#2496
Pattern
Hash Map
Reading time
2 min

The 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.

Example

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.

Python solution

Python
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)

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2496. Maximum Value of a String in an Array is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2496. Maximum Value of a String in an Array?
LeetCode 2496. Maximum Value of a String in an Array is rated Easy on LeetCode.
What topics does LeetCode 2496. Maximum Value of a String in an Array cover?
LeetCode 2496. Maximum Value of a String in an Array is tagged Array and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview