Number of Senior Citizens

Approach

  1. Iterate through the details array: You will go through each string in the array.
  2. Extract the age from each string: The age of a person is represented by the characters at indexes 11 and 12 (0-based) in each string.
  3. Count the passengers older than 60: If the extracted age is greater than 60, you increment a counter.
  4. Return the counter: The final counter will be the number of passengers who are strictly more than 60 years old.

Code

Python solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
    def countSeniors(self, details: List[str]) -> int:
        # Initialize a counter to count the number of passengers who are over 60
        count = 0

        # Iterate through the details
        for detail in details:
            # Extract the age from the string (characters 11 and 12)
            age = int(detail[11:13])

            # If the age is strictly more than 60, increment the counter
            if age > 60:
                count += 1

        return count

Explanation

  • We use string slicing to extract the age part from each string (i.e., detail[11:13]), which represents the 11th and 12th characters in the 0-indexed string.
  • We convert this substring to an integer and compare it with 60 to decide whether the passenger is older than 60.
  • We use a counter to keep track of the number of passengers who fulfill this condition, and in the end, return this count.