Swift Judge Route Circle


  • 0
    S
    class Solution {
        func judgeCircle(_ moves: String) -> Bool {
            var circle = false
            //let moveList = Array(moves.characters)
           
                let dict = moves.characters.reduce([:]) { (d, c) -> Dictionary<Character,Int> in
                    var d = d
                    let i = d[c] ?? 0
                    d[c] = i+1
                    return d
            }
            for (character, number) in dict {
                if dict["L"] == dict["R"] {
                    if dict["U"] == dict["D"]{
                        circle = true
                    }
                }
                
            }
            return circle
        }
    }
    Solution().judgeCircle("LURUDRD")
    
    

Log in to reply
 

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