[LeetCode] Maximum Value of a String in an Array

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int maximumValue(vector<string>& strs) {
long long res = 0;
for(auto& s : strs) {
bool fl = true;
for(int i = 0; i < s.length(); i++) {
if(isalpha(s[i])) fl = false;
}
if(!fl) res = max(res, (long long) s.length());
else res = max(res, stoll(s));
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/12/11/PS/LeetCode/maximum-value-of-a-string-in-an-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.