All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2601_2700.s2678_number_of_senior_citizens.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2601_2700.s2678_number_of_senior_citizens;

// #Easy #Array #String #2023_09_11_Time_0_ms_(100.00%)_Space_40.7_MB_(97.65%)

/**
 * 2678 - Number of Senior Citizens\.
 *
 * Easy
 *
 * 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.
**/
public class Solution {
    public int countSeniors(String[] details) {
        int count = 0;
        for (String detail : details) {
            if (((detail.charAt(11) - '0' == 6) && (detail.charAt(12) - '0' > 0))
                    || (detail.charAt(11) - '0' > 6)) {
                count++;
            }
        }
        return count;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy