Journal Archive

/**
 * @param {string} s
 * @return {number}
 */
var firstUniqChar = function(s) {
    let count = {}
    let ans = -1;
    for (let letter of s) {
        if (!count[letter]) {
            count[letter] = 0;
        }
        count[letter]++;
    }
    
    for (let key in count) {
        if (count[key] === 1) {
            ans = s.indexOf(key);
            break;
        }
    }
    return ans;
};

Day 89: Solving one of LeetCode problems

387. First Unique Character in a String Difficulty - Easy

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

 

Example 1:

Input: s = "leetcode"
Output: 0
		

Example 2:

Input: s = "loveleetcode"
Output: 2
		

Example 3:

Input: s = "aabb"
Output: -1