Java solution 15ms


  • 0
    L
    public class Solution {
        public int firstUniqChar(String s) {
            int freq [] = new int[26];
            char[] ch = s.toCharArray();
            
            for(int i = 0; i < ch.length; i ++)
                freq [ch[i] - 'a'] ++;
            for(int i = 0; i < ch.length; i ++)
                if(freq [ch[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.