C++ solution beats 99%


  • 0
    G
    class Solution {
    public:
        int firstUniqChar(string s) {
            if (s.size() == 0) return -1;
            
            vector<int> count(26, 0);
            size_t head = 0;
            size_t tail = 0;
            while (tail < s.size()) {
                count[s[tail++] - 'a']++;
                while (head < s.size() && count[s[head] - 'a'] > 1) {
                    head++;
                }
                if (tail < head) tail = head;
            }
            return head < s.size() ? head : -1;
        }
    };
    

Log in to reply
 

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