Javascript solution beats 77% of commits


  • 0
    J
    var wordPattern = function(pattern, str) {
        var mk = new Map(),mv = new Map(), pa = pattern.split(""), sa = str.split(" ");
        if (pa.length !== sa.length) return false;
        for (var i = 0; i < pa.length; i++) {
            if ((mk.get(pa[i]) !== undefined && mk.get(pa[i]) !== sa[i]) || 
                (mv.get(sa[i]) !== undefined && mv.get(sa[i]) !== pa[i])) return false;
            mk.set(pa[i], sa[i]);
            mv.set(sa[i], pa[i]);
        }
        return true;
    };

Log in to reply
 

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