Watch 10 video solutions for Number of Senior Citizens, a easy level problem involving Array, String. This walkthrough by NeetCodeIO has 6,458 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 | 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. |