My Java Solution using HashMap


  • 0
    A
    
    class Solution {
        public int firstUniqChar(String s) {
            
            Map<Character, Integer> map = new HashMap<Character,Integer>();
            for(Character c:s.toCharArray()){
                if(map.get(c)!=null){
                    map.put(c,map.get(c)+1);
                }
                else {
                    map.put(c,1);
                }
            }
            
            for(int i=0;i<s.length();i++){
                if(map.get(s.charAt(i))==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.