LeetCode in Kotlin

2446. Determine if Two Events Have Conflict

Easy

You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where:

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:

Solution

class Solution {
    fun haveConflict(event1: Array<String>, event2: Array<String>): Boolean {
        val aStart = getTimeSerial(event1[0])
        val aEnd = getTimeSerial(event1[1])
        val bStart = getTimeSerial(event2[0])
        val bEnd = getTimeSerial(event2[1])
        return bStart >= aStart && bStart <= aEnd || bStart <= aStart && bEnd >= aStart
    }

    private fun getTimeSerial(timestamp: String): Int {
        var hours = 0
        var minutes = 0
        var isHours = true
        var i = 0
        while (i < timestamp.length) {
            if (timestamp[i] == ':') {
                isHours = false
            } else if (isHours) {
                hours = hours * 10 + Character.getNumericValue(timestamp[i])
            } else {
                minutes = minutes * 10 + Character.getNumericValue(timestamp[i])
            }
            i += 1
        }
        return 60 * hours + minutes
    }
}