One pass O(1n) solution using arrays


  • 0
    B
    public int firstUniqChar(String s) {
    		if(s==null||s.length()==0)
    			return -1;
    		int[] loc = new int[26];
    		for(int i=0;i<loc.length;i++)
    			loc[i] = -1;
    
    		for(int i=0;i<s.length();i++){
    			int pos = s.charAt(i)-'a';
    			if(loc[pos]==-1)
    				loc[pos] = i;
    			else if(loc[pos]>=0)
    				loc[pos]=-2;
    		}
    		int min = Integer.MAX_VALUE;
    		for(int i=0;i<loc.length;i++)
    			min = loc[i]>=0?Math.min(min, loc[i]):min;
    			return min==Integer.MAX_VALUE?-1:min;
    	}
    

  • 0

    Why you claim this one pass?


Log in to reply
 

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