C++ solution use array easy understand


  • 1
    class Solution {
    public:
        int firstUniqChar(string s) {
            int map[26]={};
            for(auto x:s)
                map[x-'a']++;
            for(int i=0; i<s.size(); i++)
            {
                if(map[s[i]-'a']==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.