2678. Number of Senior Citizens - Explanation
Description
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:
- The first ten characters consist of the phone number of passengers.
- The next character denotes the gender of the person.
- The following two characters are used to indicate the age of the person.
- The last two characters determine the seat allotted to that person.
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'.- The phone numbers and seat numbers of the passengers are distinct.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- String Indexing and Slicing - Extracting characters or substrings from specific positions in a string
- String to Integer Conversion - Parsing numeric characters into integer values
- ASCII Character Arithmetic - Converting digit characters to their numeric values using ASCII codes
1. String Parsing
Intuition
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.
Algorithm
- Initialize a counter
resto0. - For each detail string
d:- Extract the substring from index
11to13(exclusive). - Parse it as an integer.
- If the value is greater than
60, incrementres.
- Extract the substring from index
- Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Character-Based Extraction
Intuition
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.
Algorithm
- Initialize a counter
resto0. - For each detail string
d:- Get the character at index
11and convert to its numeric value:ten = d[11] - '0'. - Get the character at index
12and convert to its numeric value:one = d[12] - '0'. - Compute
age = 10 * ten + one. - If
age > 60, incrementres.
- Get the character at index
- Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Off-By-One Index Errors
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.
Using Greater-Than-Or-Equal Instead of Greater-Than
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.
Sign in to join the discussion