Java Solution Using Map.


  • 0
    P
        public int firstUniqChar(String s) {
            if(s.length()==0){
                return -1;
            }
            if(s.length()==1){
                return 0;
            }
            
            Map<Character,Integer> temp = new HashMap<Character, Integer>();
            for(int i = 0 ; i < s.length(); i++){
                if(temp.containsKey(s.charAt(i))) {
                     
                   temp.put(s.charAt(i),temp.get(s.charAt(i))+1); 
                }else{
                    temp.put(s.charAt(i),1);
                }
                
            }
            
            for(int i = 0 ; i < s.length(); i++){
                if(temp.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.