Very simple C++ (C-style) solution beats +90%


  • 0
    S
    class Solution {
    public:
        int firstUniqChar(string s) {
    #define CHAR_LENGTH 'z' - 'a' + 1
            int char_list[CHAR_LENGTH] = {0};
            
            for (int i = 0; s[i]; ++i){
                ++char_list[s[i] - 'a'];
            }
            for (int i = 0; s[i]; ++i){
                if (char_list[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.