arcbjorn

thoughtbook

Monday, January 2, 2023

Student Attendance Record I

Leetcode 551 problem.

Go

  • Time complexity: O(n)O(n) - n is a length of a string
  • Auxiliary space: O(1)O(1) - constant amount of space
func checkRecord(s string) bool {
    consLateCount := 0
    absentCount := 0
    result := true

    for _, rune := range s {

        if string(rune) == "L" {
            consLateCount++
        } else {
            consLateCount = 0
        }

        if string(rune) == "A" {
            absentCount++
        }

        if consLateCount > 2 || absentCount > 1 {
            result = false
            break
        }
    }

    return result
}
Creative Commons Licence