Javascript regex solution


  • 0
    K
    var isOneBitCharacter = function(bits) {
        if(bits.length == 1) return true;
        return bits.join("").replace(/(11|10)/g, "x").slice(-1) != "x";
    };
    

    Reading the question, a regex-based solution seemed to jump out at me(since we want some kind of greedy match).

    Basically, replace all instances of the second character(greedily) with some sentinel. Then if the last character of the replaced string isn't the sentinel, then it is the first character.

    i.e.

    100 => x0 - true
    1110  => xx - false
    010 - => 0x - false
    

Log in to reply
 

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