Number of Senior Citizens — LeetCode 2678 Python Solution

EasyArrayString
Problem
#2678
Pattern
Hash Map
Reading time
2 min

The problem

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.

Example

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.

Python solution

Python
class Solution:
    def countSeniors(self, details: List[str]) -> int:
        return sum(int(x[11:13]) > 60 for x in details)

Complexity

MeasureComplexity
TimeO(n), where n is the length of `details`
Space$O(1)` auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2678. Number of Senior Citizens is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2678. Number of Senior Citizens?
LeetCode 2678. Number of Senior Citizens is rated Easy on LeetCode.
What is the time complexity of LeetCode 2678. Number of Senior Citizens?
The Python solution on this page runs in O(n), where n is the length of `details`.
What is the space complexity of LeetCode 2678. Number of Senior Citizens?
The Python solution on this page uses $O(1)` auxiliary space.
What topics does LeetCode 2678. Number of Senior Citizens cover?
LeetCode 2678. Number of Senior Citizens is tagged Array and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview