LeetCode Entry

657. Robot Return to Origin

05.04.2026 easy 2026 kotlin rust

R,L,U,D return to 0 in XY plane

657. Robot Return to Origin easy

youtube

05.04.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1319

Problem TLDR

R,L,U,D return to 0 in XY plane #easy

Intuition

Compare counts separately for vertical and horizontal directions. Both directions can fit into a single variable h*2^16+v.

Approach

  • hash collisions of 8 make the solution extra spicy
  • do you know how %5 works?

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

// 38ms
    fun judgeCircle(m: String) =
        0==m.sumOf {listOf(8,-1,1,-8)[it.code%5]}
// 0ms
    pub fn judge_circle(m: String) -> bool {
        0==m.bytes().fold(0,|a,b|a+[8,-1,1,-8][(b%5)as usize])
    }