290.单词法则(javascript)290.WordPattern

原创
小哥 3年前 (2022-11-10) 阅读数 8 #大杂烩

leetcode标题来源: https://leetcode-cn.com/problems/word-pattern/
给定法律 pattern 还有一根绳子 s ,判断 s 是否遵循相同的规则。

这里的 遵循 表示完全匹配,例如, pattern 每个字母和字符串 str 双向连接中的每个非空单词之间都有对应关系。

Given a pattern and a string s, find if s follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

示例1:

输入: pattern = “abba”, str = “dog cat cat dog”
输出: true

示例 2:

输入:pattern = “abba”, str = “dog cat cat fish”
输出: false

示例 3:

输入: pattern = “aaaa”, str = “dog cat cat dog”
输出: false

Example 1:

Input: pattern = “abba”, s = “dog cat cat dog”
Output: true

Example 2:

Input: pattern = “abba”, s = “dog cat cat fish”
Output: false

Example 3:

Input: pattern = “aaaa”, s = “dog cat cat dog”
Output: false

var wordPattern = function (pattern, s) {
    var len = pattern.length, list = s.split(" ")
    if (len != list.length) {
        console.log("长度不一", len, list.length);
        return false
    }
    var hp = Object.create(null),//定义空对象pattern中的值是关键
        hs = Object.create(null);//定义空对象list中的值是关键
    for (let i = 0; i < list.length; i++) {//遍历数组
        console.log("hp.has(i)", pattern[i], list[i]);//pattern中第i项;list中的第i项
        if (hp[pattern[i]] != void 0) {//pattern[i]键在hp中存在,确定是否相等
            if (hp[pattern[i]] != list[i]) {//存在 hp的pattern[i]键值不相等list[i]
                console.log("不一致");
                return false
            }
        } else {//如果它不存在,添加它,pattern[i]为键,list[i]为值
            hp[pattern[i]] = list[i]
        }

        if (hs[list[i]] != void 0) {//list[i]键在hs中存在,确定是否相等
            if (hs[list[i]] != pattern[i]) {//存在 hs的list[i]键值不相等pattern[i]
                console.log("不一致");
                return false
            }
        } else {//如果它不存在,添加它,list[i]为键,pattern[i]为值
            hs[list[i]] = pattern[i]
        }
    }
    return true

};

优化代码

var wordPattern = function (pattern, s) {
    var len = pattern.length, list = s.split(" ")
    if (len != list.length) return false
    var hp = Object.create(null),hs = Object.create(null);
    for (let i = 0; i < list.length; i++) {
        if (hp[pattern[i]] != void 0) {
            if (hp[pattern[i]] != list[i]) return false
        } else {
            hp[pattern[i]] = list[i]
        }

        if (hs[list[i]] != void 0) {
            if (hs[list[i]] != pattern[i]) return false
        } else {
            hs[list[i]] = pattern[i]
        }
    }
    return true

};

然后优化代码

var wordPattern = function (pattern, s) {
    var len = pattern.length, list = s.split(" ")
    if (len != list.length) return false
    var hp = Object.create(null),hs = Object.create(null);
    for (let i = 0; i < list.length; i++) {
        var p=pattern[i],l=list[i]
        if (hp[p] != void 0) {
            if (hp[p] != l) return false
        } else {
            hp[p] = l
        }

        if (hs[l] != void 0) {
            if (hs[l] != p) return false
        } else {
            hs[l] = p
        }
    }
    return true

};
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除