Skip to content

2496. Maximum Value of a String in an Array 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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;
  }
};
1
2
3
4
5
6
7
8
9
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;
  }
}
1
2
3
4
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)