C++ Concise and Easy Understanding Solution


  • 1

    The code

    class Solution {
    public:
        int firstUniqChar(string s) {
            int cnt[256] = {0};
            for (char c : s) cnt[c]++;
            for (int i = 0; i < s.length(); i++) {
                if (cnt[s[i]] == 1) return i;
            } 
            return -1;
        }
    };
    

  • 0
    S

    int cnt[26] ={0} is enough.
    all the inputs are lower cases.


Log in to reply
 

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