Simple C++ solution


  • 0
    U
    class Solution {
    public:
        
        int firstUniqChar(string s) 
        {
            // Map is an ordered data structure. It maintains the order
            map<char,int> countMap;
            map<int,char> indexMap;
            
            for(int i=0; i<s.length(); i++)
            {
                countMap[s[i]]++;
                indexMap[i] = s[i];
            }
            
            int index;
            for(auto it = indexMap.begin(); it != indexMap.end(); ++it)
            {
                index = (*it).first;
                if(countMap[indexMap[index]] == 1)
                {
                    return index;
                }
            }
            return -1;
        }
    };
    

Log in to reply
 

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