minjs1cn / weekly-learning

每周学习分享打卡
0 stars 0 forks source link

34 -【leetcode】同构字符串 #34

Open Guyidingsanyu opened 3 years ago

Guyidingsanyu commented 3 years ago

同构字符串

xieshiyi commented 3 years ago
/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isIsomorphic = function(s, t) {
    if(s.length !== t.length) {
        return false
    }
    let s2t = {}
    let t2s = {}
    for(let i = 0; i < s.length; i++) {
        const x = s[i]
        const y = t[i]
       // 若已经存在映射,但是对应哈希表中的值却不一致,则return false
        if((s2t[x] && s2t[x] !== y) || t2s[y] && t2s[y] !== x) {
            return false
        }
        s2t[x] = y
        t2s[y] = x
    }
    return true
};