NeetCode #35LC-2678EasyArrays & Hashing
← Back to All Problems#35 · #2678 · Number of Senior Citizens(老人的数目)
📌 Problem Statement & Constraints
Given an array
details of passenger information strings, each formatted as "<phone number><gender><age><seat>", return the number of passengers who are strictly over 60 years old. The age occupies fixed character positions 11 and 12. Constraints: 1 <= details.length <= 100, details[i].length == 15.💡 Core Algorithmic Approaches
- Because every string has the same fixed layout, the age is always at indices 11 and 12 (0-based).
- Slice those two characters and convert to an integer.
- Count how many exceed 60.
- Splitting on spaces is a valid alternative, but the fixed-width slice is O(1) per string and directly reflects the stated format.
💻 Benchmark Python3 Implementation
class Solution:
def countSeniors(self, details: List[str]) -> int:
return sum(1 for d in details if int(d[11:13]) > 60)⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): one slice and comparison per passenger.
💾 Space Complexity
O(1): a single accumulator.
⚠️ Interview Pitfalls & Follow-ups
- Using
>= 60: the problem says strictly over 60, so 60 itself does not count. - Guessing the wrong slice indices: the phone number is 10 digits, then the gender is 1 character, so the age starts at index 11 and spans two characters.
- Splitting on spaces and indexing the wrong field: the format is fixed-width, and the phone number contains no spaces, so the slice is more robust than a split-based parse here.