[JavaScript ] Using Hashmap with time complexity O(n) and space complexity O(1)


  • 0
    A

    '''
    /**

    • @param {string} s
    • @return {number}
      */
      var firstUniqChar = function(s) {
      var len=s.length;
      var hash={};
      for(var i=0;i<len;i++){
      if(hash[s.charAt(i)]==undefined){
      hash[s.charAt(i)]=1;
      }else{
      hash[s.charAt(i)]++;
      }
      }
      for(var i=0;i<len;i++){
      if(hash[s.charAt(i)]===1)
      return i;
      }
      return -1;
      };
      '''

Log in to reply
 

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