Easy
The value of an alphanumeric string can be defined as:
10
, if it comprises of digits only.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:
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.public class Solution {
public int maximumValue(String[] strs) {
int maxVal = 0;
for (String s : strs) {
maxVal = Math.max(maxVal, value(s));
}
return maxVal;
}
private int value(String s) {
int total = 0;
for (char ch : s.toCharArray()) {
if (ch >= '0' && ch <= '9') {
total = total * 10 + (ch - '0');
} else {
return s.length();
}
}
return total;
}
}