2409. Count Days Spent Together LeetCode Solution
In this guide, you will get 2409. Count Days Spent Together LeetCode Solution with the best time and space complexity. The solution to Count Days Spent Together problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Days Spent Together solution in C++
- Count Days Spent Together solution in Java
- Count Days Spent Together solution in Python
- Additional Resources

Problem Statement of Count Days Spent Together
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). Each will be a 5-character string in the format “MM-DD”, corresponding to the month and day of the date.
Return the total number of days that Alice and Bob are in Rome together.
You can assume that all dates occur in the same calendar year, which is not a leap year. Note that the number of days per month can be represented as: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].
Example 1:
Input: arriveAlice = “08-15”, leaveAlice = “08-18”, arriveBob = “08-16”, leaveBob = “08-19”
Output: 3
Explanation: Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.
Example 2:
Input: arriveAlice = “10-01”, leaveAlice = “10-31”, arriveBob = “11-01”, leaveBob = “12-31”
Output: 0
Explanation: There is no day when Alice and Bob are in Rome together, so we return 0.
Constraints:
All dates are provided in the format “MM-DD”.
Alice and Bob’s arrival dates are earlier than or equal to their leaving dates.
The given dates are valid dates of a non-leap year.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2409. Count Days Spent Together LeetCode Solution in C++
class Solution {
public:
int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob,
string leaveBob) {
const int arriveA = toDays(arriveAlice);
const int leaveA = toDays(leaveAlice);
const int arriveB = toDays(arriveBob);
const int leaveB = toDays(leaveBob);
int ans = 0;
for (int day = 1; day <= 365; ++day)
if (arriveA <= day && day <= leaveA && arriveB <= day && day <= leaveB)
++ans;
return ans;
}
private:
const vector<int> days{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int toDays(const string& s) {
const int month = (s[0] - '0') * 10 + (s[1] - '0');
const int day = (s[3] - '0') * 10 + (s[4] - '0');
int prevDays = 0;
for (int m = 1; m < month; ++m)
prevDays += days[m];
return prevDays + day;
}
};
/* code provided by PROGIEZ */
2409. Count Days Spent Together LeetCode Solution in Java
class Solution {
public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob,
String leaveBob) {
final int arriveA = toDays(arriveAlice);
final int leaveA = toDays(leaveAlice);
final int arriveB = toDays(arriveBob);
final int leaveB = toDays(leaveBob);
int ans = 0;
for (int day = 1; day <= 365; ++day)
if (arriveA <= day && day <= leaveA && arriveB <= day && day <= leaveB)
++ans;
return ans;
}
private final int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private int toDays(final String s) {
final int month = (s.charAt(0) - '0') * 10 + (s.charAt(1) - '0');
final int day = (s.charAt(3) - '0') * 10 + (s.charAt(4) - '0');
int prevDays = 0;
for (int m = 1; m < month; ++m)
prevDays += days[m];
return prevDays + day;
}
}
// code provided by PROGIEZ
2409. Count Days Spent Together LeetCode Solution in Python
class Solution:
def countDaysTogether(
self,
arriveAlice: str,
leaveAlice: str,
arriveBob: str,
leaveBob: str,
) -> int:
days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def toDays(s: str) -> int:
month = int(s[:2])
day = int(s[3:])
prevDays = 0
for m in range(1, month):
prevDays += days[m]
return prevDays + day
arriveA = toDays(arriveAlice)
leaveA = toDays(leaveAlice)
arriveB = toDays(arriveBob)
leaveB = toDays(leaveBob)
ans = 0
for day in range(1, 366):
if arriveA <= day and day <= leaveA and arriveB <= day and day <= leaveB:
ans += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.