C++ Easy to understand solution (72ms) O(n)


  • 0
    N
        unordered_map<char,int> map; //Create a hash table with stl map.
        int result;
        for(int i = 0; i < input.length(); i++) //Insert in the map all characters of the string.
            map[input[i]]++;
    
        for(int j = 0; j < input.length(); j++) //Loop through again to check.
        {
            if(map[input[j]] == 1) //If character is already in the map, then it is the unique one.
            {
            result = j;
            return result;
            }       
        } 
    return -1;
    }

Log in to reply
 

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