2496. Maximum Value of a String in an Array LeetCode Solution

In this guide, you will get 2496. Maximum Value of a String in an Array LeetCode Solution with the best time and space complexity. The solution to Maximum Value of a String in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Value of a String in an Array solution in C++
  4. Maximum Value of a String in an Array solution in Java
  5. Maximum Value of a String in an Array solution in Python
  6. Additional Resources
2496. Maximum Value of a String in an Array LeetCode Solution image

Problem Statement of Maximum Value of a String in an Array

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.

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 <= 100
1 <= strs[i].length <= 9
strs[i] consists of only lowercase English letters and digits.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2496. Maximum Value of a String in an Array LeetCode Solution in C++

class Solution {
 public:
  int maximumValue(vector<string>& strs) {
    int ans = 0;
    for (const string& s : strs)
      ans = max(ans, ranges::any_of(s, [](char c) { return isalpha(c); })
                         ? static_cast<int>(s.length())
                         : stoi(s));
    return ans;
  }
};
/* code provided by PROGIEZ */

2496. Maximum Value of a String in an Array LeetCode Solution in Java

class Solution {
  public int maximumValue(String[] strs) {
    int ans = 0;
    for (final String s : strs)
      ans = Math.max(ans, s.chars().anyMatch(c -> Character.isAlphabetic(c)) ? s.length()
                                                                             : Integer.valueOf(s));
    return ans;
  }
}
// code provided by PROGIEZ

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

class Solution:
  def maximumValue(self, strs: list[str]) -> int:
    return max(len(s) if any(c.isalpha() for c in s) else int(s)
               for s in strs)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.