Watch 10 video solutions for Determine if Two Events Have Conflict, a easy level problem involving Array, String. This walkthrough by Bro Coders has 1,546 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where:
event1 = [startTime1, endTime1] andevent2 = [startTime2, endTime2].Event times are valid 24 hours format in the form of HH:MM.
A conflict happens when two events have some non-empty intersection (i.e., some moment is common to both events).
Return true if there is a conflict between two events. Otherwise, return false.
Example 1:
Input: event1 = ["01:15","02:00"], event2 = ["02:00","03:00"] Output: true Explanation: The two events intersect at time 2:00.
Example 2:
Input: event1 = ["01:00","02:00"], event2 = ["01:20","03:00"] Output: true Explanation: The two events intersect starting from 01:20 to 02:00.
Example 3:
Input: event1 = ["10:00","11:00"], event2 = ["14:00","15:00"] Output: false Explanation: The two events do not intersect.
Constraints:
event1.length == event2.length == 2event1[i].length == event2[i].length == 5startTime1 <= endTime1startTime2 <= endTime2HH:MM format.Problem Overview: You are given two events where each event contains a start and end time in HH:MM format. The goal is to determine whether the two time intervals overlap. If any portion of the time ranges intersects, the events are considered to be in conflict.
Approach 1: Convert Times to Minutes (O(1) time, O(1) space)
This approach converts each HH:MM time string into the total number of minutes from midnight. Split the string into hours and minutes, compute hours * 60 + minutes, and store the numeric values. Once both events are represented as integer ranges, checking overlap becomes a simple interval comparison: two events conflict if start1 ≤ end2 and start2 ≤ end1. This technique removes string handling complexity and turns the problem into a basic numeric comparison.
Because each event contains only two timestamps, the computation runs in constant time. This approach is often preferred when working with time calculations or when the problem later expands to require arithmetic on times. The method relies only on basic operations on a small array of values and simple parsing from a string.
Approach 2: Direct String Comparison (O(1) time, O(1) space)
The HH:MM format is lexicographically ordered. That means standard string comparison already preserves chronological order. Instead of converting to integers, you can directly compare the time strings. The same interval overlap rule applies: if event1.start ≤ event2.end and event2.start ≤ event1.end, the events conflict.
This works because the format always contains two digits for hours and minutes, ensuring correct lexical ordering. For example, "09:30" < "10:00" evaluates correctly with normal string comparison. This method avoids parsing entirely and produces a clean, minimal solution with just a few comparisons.
Since each comparison involves fixed-length strings and there are only two intervals, the time complexity remains constant. Space complexity is also constant because no additional structures are required.
Recommended for interviews: Interviewers typically expect the interval-overlap insight first: two intervals intersect unless one ends before the other begins. Either implementation is acceptable. Converting times to minutes demonstrates clear reasoning about time representation, while direct string comparison shows awareness of how formatted timestamps behave lexicographically. Mentioning both approaches shows strong problem-solving depth.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Convert Times to Minutes | O(1) | O(1) | When you want numeric comparison or expect additional time calculations |
| Direct String Comparison | O(1) | O(1) | When timestamps are guaranteed to follow HH:MM format and quick comparison is sufficient |