JavaScript solution


  • 0
    1

    var judgeCircle = function(moves) {
    if(moves.length%2 === 0){
    var a = checkLetterNum('U', moves);
    var b = checkLetterNum('D', moves);
    var c = checkLetterNum('L', moves);
    var d = checkLetterNum('R', moves);
    if(a === b && c === d){
    return true;
    }
    }
    return false;
    };

    function checkLetterNum(ch, str){
    var regex = new RegExp(ch, 'g'); // 使用g表示整个字符串都要匹配
    var result = str.match(regex);
    return !result ? 0 : result.length;
    }


Log in to reply
 

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