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: 2 Explanation: 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: 0 Explanation: 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'.Problem Overview: Each passenger record is a 15‑character string containing phone number, gender, age, and seat number. The age appears at positions 11 and 12. You need to scan the array of records and count how many passengers are older than 60.
Approach 1: String Substring Extraction (O(n) time, O(1) space)
Iterate through the input array and extract the age portion of each record using a substring operation. Since the age is stored in characters s[11:13], convert that substring into an integer and check whether it is greater than 60. If it is, increment a counter. This approach relies on standard string slicing and integer parsing, which keeps the implementation clean and easy to read. Time complexity is O(n) because you scan each record once, and space complexity is O(1) since only a few variables are used.
This method works well when readability matters and you want straightforward string handling. It uses basic operations from string processing and simple iteration over an array. Most developers reach for this version first because the intent is obvious: extract the age substring and compare it.
Approach 2: Character-Based Calculation (O(n) time, O(1) space)
Instead of creating a substring, compute the age directly from the characters. The tens digit is at s[11] and the ones digit at s[12]. Convert them to numbers and compute age = (s[11] - '0') * 10 + (s[12] - '0'). Then check whether the computed age is greater than 60. This avoids substring allocation and integer parsing.
The key insight is that the data format is fixed length, so you can access the age digits directly. That makes the solution slightly more efficient in practice while keeping the same asymptotic complexity: O(n) time for scanning the records and O(1) extra space. This technique shows up often in problems involving encoded strings or fixed‑format records.
Recommended for interviews: Both approaches pass easily because the problem is mostly about recognizing the fixed position of the age field. Start with substring extraction to demonstrate understanding of the format. The character‑based calculation shows stronger attention to detail and avoids extra parsing work, which interviewers often appreciate when discussing optimization in string manipulation tasks.
Approach: Extract the age from each detail string by selecting the appropriate substring. Convert this substring to an integer and count how many such integers are greater than 60.
The given details string has a fixed format. We know from the problem description that the age of the person is stored between the 11th and 12th character indices. By iterating over each string, extracting these characters, converting them to a number, and then checking if the number is greater than 60, we can count the number of senior citizens.
This function iterates through each string in the list, extracts the age substring (at indices 11 to 12), converts it to an integer, and checks if it is greater than 60. If so, it increments the counter senior_count.
The time complexity is O(n) where n is the number of entries in details.
The space complexity is O(1) as we are using a fixed amount of additional space.
Approach: Instead of using string slicing or substrings, calculate the age by examining the individual characters and converting them into the age. This approach does not explicitly create a substring but directly works with character indices and mathematical operations.
This avoids the creation of additional strings and might be beneficial in languages where string manipulation is costly.
Here, we manually form the age by converting both age characters at indices 11 and 12 to integers and then calculating the age. We increment senior_count if the calculated age is greater than 60.
The time complexity is O(n) due to single traversal through details. The space complexity remains O(1).
We can traverse each string x in details and convert the 12th and 13th characters (indexed at 11 and 12) of x to integers, and check if they are greater than 60. If so, we add one to the answer.
After the traversal, we return the answer.
The time complexity is O(n), where n is the length of details. The space complexity is $O(1)`.
| Approach | Complexity |
|---|---|
| String Substring Extraction | The time complexity is |
| Character-Based Calculation | The time complexity is |
| Traversal and Counting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Substring Extraction | O(n) | O(1) | Best for readability and quick implementation using built-in substring and integer parsing. |
| Character-Based Calculation | O(n) | O(1) | Preferred when avoiding substring creation or parsing overhead in fixed-format strings. |
Number of Senior Citizens - Leetcode 2678 - Python • NeetCodeIO • 6,458 views views
Watch 9 more video solutions →Practice Number of Senior Citizens with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor