2 maps simple javascript


  • 0
    R

    Simple javascript solution using 2 maps:

    /**
     * @param {string} pattern
     * @param {string} str
     * @return {boolean}
     */
    var wordPattern = function(pattern, str) {
        var map1 = {};
        var map2 = {};
        if(pattern.split("").length !== str.split(" ").length) return false;
        for( var i =0 ; i < pattern.length ; i++){
        	if(!map1[pattern.split("")[i]] && !map2[str.split(" ")[i]]){
    	    	map1[pattern.split("")[i]] = str.split(" ")[i];
    	    	map2[str.split(" ")[i]] = pattern.split("")[i];
        	}else if(map1[pattern.split("")[i]] !== str.split(" ")[i]){
        		return false;
        	}
        }
        return true;
    };
    
    

  • 0
    C

    The solution was so smart. But the style of your code just so-so.


Log in to reply
 

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