pwstrick / daily

一份搜集的前端面试题目清单、面试相关以及各类学习的资料(不局限于前端)
2.39k stars 242 forks source link

找不同 #1033

Open pwstrick opened 4 years ago

pwstrick commented 4 years ago

389. 找不同

/**
 * @param {string} s
 * @param {string} t
 * @return {character}
 */
var findTheDifference = function(s, t) {
    const len1 = s.length,
        len2 = t.length;
    let one = 0;
    for(let i=0; i<len1; i++) {
        one ^= (s[i].charCodeAt() ^ t[i].charCodeAt())
    }
    one ^= t[len2 - 1].charCodeAt();
    return String.fromCharCode(one);
};