my javascript solution


  • 0
    Z
    /**
     * @param {string} pattern
     * @param {string} str
     * @return {boolean}
     */
    function wordPattern(pattern, str) {
        const arr = str.split(" ");
        if (pattern.length !== arr.length) {
            return false;
        }
        for (let i = 0; i < pattern.length; i++) {
            for (let j = i + 1; j < pattern.length; j++) {
                if (!(pattern[i] === pattern[j]) === (arr[i] === arr[j])) {
                    return false;
                }
            }
        }
        return true;
    }
    

Log in to reply
 

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