C++ two traverse O(n) solution


  • 0
    A
    class Solution {
    public:
        int firstUniqChar(string s) {
            vector<int> f(26, 0);
            for (char c : s)
                ++f[c - 'a'];
            
            for (int i = 0; i < s.length(); ++i) {
                if (f[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.