Easy java solution with queue,not fastest,can be improved


  • 0
    D
    public int firstUniqChar( String s ) {
           Queue<Character> queue = new LinkedList<>();
           int[] count = new int[26];
           if(s.length() < 1) return -1;
           char[] chs = s.toCharArray();
           for(int i=0;i<chs.length;i++){
        	   if( count[chs[i]-'a'] == 0){
        		   queue.add( chs[i] );
        		   count[chs[i]-'a']++;
        	   }else if(!queue.isEmpty()){
        	       queue.remove( (Character)chs[i] );
        	   }
           }
           if(queue.isEmpty()){
        	   return -1;
           }
           return s.indexOf( queue.peek() );
    }
    

Log in to reply
 

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