C++ 46ms 4 lines. Easy to understand


  • 2
    R

    My name is Rebecca. A girl loving sharing ideas.
    Please let me know if you like this. :-P

    class Solution {
    public:
        int firstUniqChar(string s) {
            int cnt[26] = {0} , ans = -1,j = 0;
            for(int i = 0; i < s.length(); i++)  cnt[s[i] - 'a']++;
            for(j; j < s.length() && cnt[s[j] - 'a'] != 1; j++);
            return j == s.length() ? -1 : j;
        }
    };
    

  • 0
    V

    @RebeccaLiu I don't think it'll work for case sensitive strings. But good job with a simple and fast solution.


  • 0
    L

    @RebeccaLiu Neat and beautiful without any fancy usage of stl, awsome!


Log in to reply
 

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