You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:
Return the number of passengers who are strictly more than 60 years old.
Example 1:
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
Output: 2Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
Example 2:
Input: details = ["1313579440F2036","2921522980M5644"]
Output: 0Explanation: None of the passengers are older than 60.
Constraints:
1 <= details.length <= 100details[i].length == 15details[i] consists of digits from '0' to '9'.details[i][10] is either 'M' or 'F' or 'O'.Before attempting this problem, you should be comfortable with:
Each passenger detail string has a fixed format where the age is encoded at positions 11 and 12 (0-indexed). We need to extract these two characters as a substring, convert them to an integer, and check if the age exceeds 60. This is a straightforward string slicing operation.
res to 0.d:11 to 13 (exclusive).60, increment res.res.Instead of creating a substring and parsing it, we can directly extract the two digit characters and compute the age mathematically. By subtracting the ASCII value of '0' from each character, we get the numeric value of that digit. The tens digit is at index 11 and the ones digit is at index 12. Combining them gives us the age without any string allocation overhead.
res to 0.d:11 and convert to its numeric value: ten = d[11] - '0'.12 and convert to its numeric value: one = d[12] - '0'.age = 10 * ten + one.age > 60, increment res.res.The age is located at indices 11 and 12 (0-indexed). A common mistake is using the wrong indices, such as 10 and 11, or forgetting that substring methods in many languages use exclusive end indices. Always verify the exact positions based on the problem's string format specification.
The problem asks for passengers strictly older than 60, not 60 or older. Using >= 60 instead of > 60 will incorrectly count 60-year-olds as senior citizens.