Satisfiability of Equality Equations-LeetCode

PHOTO EMBED

Thu Oct 13 2022 15:56:42 GMT+0000 (Coordinated Universal Time)

Saved by @j_jivan #javascript

var equationsPossible = function (equations) {
    const Obj = {};
    for (let i = 97, j = 1; i < 124; ++i){
        const char = String.fromCharCode(i);
        Obj[char] = j++;
    }
    equations.forEach(ele => {
        if (ele[1] === '=') {
            if (Obj[ele[0]] != Obj[ele[3]]) {
                const a = Obj[ele[0]]
                const b = Obj[ele[3]]
                for (const key in Obj) {
                    if (Obj[key] === a) {
                        Obj[key] = b;
                    }
                }
            }
        }
    });
    console.log(Obj);
    for (let i = 0; i < equations.length; ++i){
        if (equations[i][1] === '!') {
           if (Obj[equations[i][0]] === Obj[equations[i][3]]) {
             return false;
           } 
        }
    }
    return true;
};
content_copyCOPY

Satisfiability of Equality Equations

https://leetcode.com/submissions/detail/821702627/