Difficulty: Easy | Acceptance: 53.20% | Paid: No Topics: Array, String
You are given two arrays of strings event1 and event2 where:
event1 = [startTime1, endTime1]
event2 = [startTime2, endTime2]
startTime is always strictly less than endTime.
The format of time is “HH:MM”.
Two events have a conflict if the start time of one event is less than or equal to the end time of the other event, and the end time of that event is greater than or equal to the start time of the other event. Basically, if they overlap or touch.
Return true if there is a conflict, otherwise false.
- Examples
- Constraints
- String Comparison
- Integer Conversion
Examples
Input: event1 = ["01:15","02:00"], event2 = ["02:00","03:00"]
Output: true
Explanation: The two events overlap at time 2:00.
Input: event1 = ["01:00","02:00"], event2 = ["01:20","03:00"]
Output: true
Explanation: The two events overlap from 01:20 to 02:00.
Input: event1 = ["10:00","11:00"], event2 = ["12:00","13:00"]
Output: false
Explanation: The two events do not overlap.
Constraints
event1.length == event2.length == 2
event1[i].length == event2[i].length == 5
startTime1 <= endTime1
startTime2 <= endTime2
All the event times follow the HH:MM format.
String Comparison
Intuition Since the time format is strictly “HH:MM”, lexicographical string comparison works perfectly to determine chronological order (e.g., “01:15” < “02:00”). We can check for non-overlapping conditions and negate the result.
Steps
- Check if
event1ends beforeevent2starts (event1[1] < event2[0]). - Check if
event2ends beforeevent1starts (event2[1] < event1[0]). - If neither is true, there is a conflict.
class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
return not (event1[1] < event2[0] or event2[1] < event1[0])Complexity
- Time: O(1)
- Space: O(1)
- Notes: Very efficient due to fixed string length and direct comparison.
Integer Conversion
Intuition Convert the “HH:MM” time strings into total minutes since midnight. This transforms the problem into a standard integer interval overlap check.
Steps
- Parse hours and minutes for start and end times of both events.
- Calculate total minutes:
hours * 60 + minutes. - Check if the maximum start time is less than or equal to the minimum end time.
class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
def get_minutes(t):
h, m = map(int, t.split(':'))
return h * 60 + m
s1, e1 = get_minutes(event1[0]), get_minutes(event1[1])
s2, e2 = get_minutes(event2[0]), get_minutes(event2[1])
return max(s1, s2) <= min(e1, e2)Complexity
- Time: O(1)
- Space: O(1)
- Notes: Slightly more verbose but conceptually clear for interval arithmetic.