Simpe Java solution


  • 0
    M
    public int firstUniqChar(String s) {
            int[] cache = new int[26];
            for (char c : s.toCharArray()) cache[c - 'a']++;
            for (int i = 0; i < s.length(); i++) {
                if (cache[s.charAt(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.