javascript solution with hashmap


  • 0
    H

    /**

    • @param {string} s

    • @param {string} t

    • @return {character}
      */
      var findTheDifference = function(s, t) {
      var stable={};
      var ttable={};
      var arr=[];
      s=s.split("");
      t=t.split("");
      var i;
      for (i=0;i<s.length;i++){
      if(!stable[s[i]]){
      stable[s[i]]=1;
      } else{
      stable[s[i]]++;
      }
      }
      for (i=0;i<t.length;i++){
      if(!ttable[t[i]]){
      ttable[t[i]]=1;
      }else{
      ttable[t[i]]++;
      }
      }
      for(var a in ttable){
      if(a in stable){
      if(stable[a]!=ttable[a]){
      arr.push(a);
      }
      }else{
      arr.push(a);
      }

      }
      return arr;

    };


  • 0
    var findTheDifference = function(s, t) {
        const hashMap = {};
        for (var i = 0, l = s.length; i < l; i++) {
            if (!hashMap[s[i]]) {
                hashMap[s[i]] = 1;
            } else {
                hashMap[s[i]]++;
            }
        }
        for (i = 0, l++; i < l; i++){
            if (!hashMap[t[i]]) {
                return t[i];
            } else {
                hashMap[t[i]]--;
            }
        }
    };

Log in to reply
 

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