Skip to main content
Back to problems
#2409
Easy Algorithms

Count days spent together

Math String
47.6% acceptance
Feb 25, 2026
283
593
Alice and Bob are traveling to Rome for separate business meetings. You are given 4 strings arriveAlice, leaveAlice, arriveBob, and leaveBob. Alice will be in the city from the dates arriveAlice to leaveAlice (inclusive), while Bob will be in the city from the dates arriveBob to leaveBob (inclusive). Before their trips, you are asked to count the number of days when both Alice and Bob are in Rome together. Formally, return the number of days when Alice and Bob are both in Rome. All dates are provided as strings in the format "MM-DD". It is guaranteed that all dates are valid dates on a non-leap year (365 day year).

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_days_together(
    arrive_alice: String,
    leave_alice: String,
    arrive_bob: String,
    leave_bob: String,
  ) -> i32 {
    let months = [31i32, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    let to_day = |s: &str| -> i32 {
      let parts: Vec<i32> = s.split('-').map(|x| x.parse().unwrap()).collect();
      let m = parts[0] as usize;
      let d = parts[1];
      months[..m - 1].iter().sum::<i32>() + d
    };

    let aa = to_day(&arrive_alice);
    let la = to_day(&leave_alice);
    let ab = to_day(&arrive_bob);
    let lb = to_day(&leave_bob);

    let start = aa.max(ab);
    let end = la.min(lb);

    (end - start + 1).max(0)
  }
}