2678. Number of Senior Citizens - Explanation

Problem Link

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: 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 <= 100
  • details[i].length == 15
  • details[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

Company Tags

Please upgrade to NeetCode Pro to view company tags.



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

  1. Initialize a counter res to 0.
  2. For each detail string d:
    • Extract the substring from index 11 to 13 (exclusive).
    • Parse it as an integer.
    • If the value is greater than 60, increment res.
  3. Return res.
class Solution:
    def countSeniors(self, details: List[str]) -> int:
        res = 0
        for d in details:
            if int(d[11:13]) > 60:
                res += 1
        return res

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

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

  1. Initialize a counter res to 0.
  2. For each detail string d:
    • Get the character at index 11 and convert to its numeric value: ten = d[11] - '0'.
    • Get the character at index 12 and convert to its numeric value: one = d[12] - '0'.
    • Compute age = 10 * ten + one.
    • If age > 60, increment res.
  3. Return res.
class Solution:
    def countSeniors(self, details: List[str]) -> int:
        res = 0
        for d in details:
            ten = ord(d[11]) - ord("0")
            one = ord(d[12]) - ord("0")
            age = one + 10 * ten
            if age > 60:
                res += 1
        return res

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1)

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.