Very short Python and Ruby


  • 2

    Python:

    def judgeCircle(self, moves):
        return not sum(1j**'RUL'.find(m) for m in moves)
    

    Ruby:

    def judge_circle(moves)
      moves.chars.map { |m| 1i**"RULD".index(m) }.sum == 0
    end
    

    Had posted the Python version before as a reply to @waigx.


  • 0

    Another one in Ruby, using a recursive regular expression... I think it's correct, but unfortunately it's too slow and gets Time Limit Exceeded. But I find it interesting anyway.

    def judge_circle(moves)
      moves.chars.sort.join.match?(/^(|D\g<1>U|L\g<1>R)$/)
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.