Number of Senior Citizens (LeetCode 2678): Solution and Explanation
Each passenger is a fixed 15-character string holding a phone number, a gender letter, a two-digit age and a seat. Count the passengers whose age is strictly greater than 60.
Intuition
The string is a record with a fixed layout, like a boarding pass. The age is always printed in the same two boxes: indices 11 and 12, right after the gender letter.
So there is nothing to search for. Jump straight to those two characters, turn them into a number, and compare it with 60.
The comparison is strict. A passenger who is exactly 60 is not counted.
Approach: Fixed-position field read
- Set
count = 0. - For each string
d, computeagefromd[11]andd[12](tens digit times 10 plus ones digit, or parse the sliced[11:13]). - If
age > 60, add 1 tocount. - Return
count.
Step-by-step example
details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
- Ticket 0: characters 11 and 12 are "75". 75 > 60, so count becomes 1.
- Ticket 1: characters 11 and 12 are "92". 92 > 60, so count becomes 2.
- Ticket 2: characters 11 and 12 are "40". 40 is not over 60, so count stays 2.
- Return 2.
Code: Python, Java, C++
1class Solution:
2 def countSeniors(self, details: List[str]) -> int:
3 count = 0
4 for d in details:
5 age = int(d[11:13])
6 if age > 60:
7 count += 1
8 return count1class Solution {
2 public int countSeniors(String[] details) {
3 int count = 0;
4 for (String d : details) {
5 int age = (d.charAt(11) - '0') * 10 + (d.charAt(12) - '0');
6 if (age > 60) {
7 count++;
8 }
9 }
10 return count;
11 }
12}1class Solution {
2public:
3 int countSeniors(vector<string>& details) {
4 int count = 0;
5 for (const string& d : details) {
6 int age = (d[11] - '0') * 10 + (d[12] - '0');
7 if (age > 60) {
8 count++;
9 }
10 }
11 return count;
12 }
13};Time and space complexity
O(n)
Each of the n strings is visited once and only two of its characters are read.
O(1)
Only the counter and the current age are stored.
Other approaches
Parse with a slice (time O(n), space O(1))
Use int(d[11:13]) or Number(d.slice(11, 13)). Shorter to write; it creates a tiny two-character string per passenger.
Compare the characters directly (time O(n), space O(1))
An age is over 60 when the tens digit is above '6', or it is '6' and the ones digit is above '0'. This skips number conversion entirely.
Common mistakes
- Reading the wrong indices. Index 10 is the gender letter, so the age starts at 11, not 10.
- Using
>= 60. The problem asks for strictly more than 60. - Comparing the age as a string with "60" in a language where that is not a lexicographic comparison of two equal-length strings.
Frequently asked questions
Why can the age be read without parsing the rest of the string?
Every string has the same 15-character layout, so the age is always at indices 11 and 12. The phone number, gender and seat never affect where it sits.
Is a passenger aged 60 a senior here?
No. Only ages strictly greater than 60 count.
Can the age have a leading zero?
Yes. Ages under 10 are written with a leading zero such as "07", which still parses to 7.