-
Notifications
You must be signed in to change notification settings - Fork 0
/
2496_maximum_value_of_a_string_in_an_array.cpp
56 lines (44 loc) · 1.57 KB
/
2496_maximum_value_of_a_string_in_an_array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//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.
class Solution {
public:
int maximumValue(vector<string>& strs) {
int maxValue = 0;
for (string str : strs) {
int value = calculateValue(str);
maxValue = max(maxValue, value);
}
return maxValue;
}
private:
int calculateValue(const string &str) {
int value = 0;
bool containsDigits = false;
for (char c : str) {
if (isdigit(c)) {
value = value * 10 + (c - '0');
containsDigits = true;
} else {
return str.length();
}
}
return containsDigits ? value : str.length();
}
};