Maximum Value of a String in an Array — LeetCode 2496 Python Solution
- Problem
- #2496
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
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
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
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(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.