arcbjorn

thoughtbook

RSS Feed

Monday, January 3, 2022

Robot return to origin

Leetcode 657 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
import "strings"

func judgeCircle(moves string) bool {
    ups := strings.Count(moves, "U")
    downs := strings.Count(moves, "D")

    if ups != downs {
        return false
    }

    lefts := strings.Count(moves, "L")
    rights := strings.Count(moves, "R")

    if lefts != rights {
        return false
    }


    return true
}
Creative Commons Licence