Difficulty: Easy | Acceptance: 81.20% | Paid: No Topics: Array, String
You are given a 0-indexed array of strings details. Each element of details provides information about a passenger in a bus. The details[i] string is in the format “<phoneNumber><age><gender>” where:
- phoneNumber is a string of digits representing the phone number.
- age is a string of digits representing the age of the passenger.
- gender is a string of characters representing the gender of the passenger.
The phone number, age, and gender are concatenated together without any separators.
A passenger is considered a senior citizen if their age is strictly greater than 60.
Return the number of senior citizens on the bus.
- Examples
- Constraints
- String Slicing
- Character Comparison
- Regular Expression
Examples
Example 1
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
Output: 2
Explanation: The passengers at indices 0 and 2 are senior citizens because their ages are 75 and 40 respectively, but only 75 > 60.
Example 2
Input: details = ["1313579440F2036","2921522980M5644"]
Output: 0
Explanation: None of the passengers are senior citizens.
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.
String Slicing
Intuition Extract the age substring from each details string and convert it to an integer for comparison with 60.
Steps
- Initialize a counter to 0
- For each detail string, extract characters at indices 11 and 12 (the age portion)
- Convert the extracted substring to an integer
- If the age is greater than 60, increment the counter
- Return the counter
class Solution:
def countSeniors(self, details: list[str]) -> int:
count = 0
for detail in details:
age = int(detail[11:13])
if age > 60:
count += 1
return countComplexity
- Time: O(n) where n is the number of details strings
- Space: O(1) constant extra space
- Notes: Simple and readable approach with string slicing
Character Comparison
Intuition Compare the age characters directly without converting to integer by checking if the age is greater than “60”.
Steps
- Initialize a counter to 0
- For each detail string, check if the age portion (indices 11-12) represents a number > 60
- This can be done by checking if the first digit > ‘6’ or if first digit == ‘6’ and second digit > ‘0’
- If the condition is met, increment the counter
- Return the counter
class Solution:
def countSeniors(self, details: list[str]) -> int:
count = 0
for detail in details:
if detail[11] > '6' or (detail[11] == '6' and detail[12] > '0'):
count += 1
return countComplexity
- Time: O(n) where n is the number of details strings
- Space: O(1) constant extra space
- Notes: Avoids string to integer conversion, slightly more efficient
Regular Expression
Intuition Use regular expressions to match and count details strings where the age portion is greater than 60.
Steps
- Create a regular expression pattern that matches ages 61-99
- Count how many detail strings match this pattern
- Return the count
import re
class Solution:
def countSeniors(self, details: list[str]) -> int:
pattern = re.compile(r'^.{11}(6[1-9]|[7-9][0-9])')
return sum(1 for detail in details if pattern.match(detail))Complexity
- Time: O(n) where n is the number of details strings
- Space: O(1) constant extra space
- Notes: More verbose but demonstrates regex usage for pattern matching