LeetCode Entry

2678. Number of Senior Citizens

01.08.2024 easy 2024 kotlin rust

Count filtered by a substring

2678. Number of Senior Citizens easy blog post substack youtube 2024-08-01_08-08_1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/689

Problem TLDR

Count filtered by a substring #easy

Intuition

The 11th and 12th symbols are our target.

Approach

We can avoid Int parsing just by comparing symbols to 6 and 0.

Let’s use some API:

  • Kotlin: count, drop, take
  • Rust: string[..], parse, filter, count

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code


    fun countSeniors(details: Array<String>) =
        details.count { it.drop(11).take(2).toInt() > 60 }


    pub fn count_seniors(details: Vec<String>) -> i32 {
        details.iter().filter(|s|
            s[11..13].parse::<u8>().unwrap() > 60
        ).count() as _
    }