Java code 25ms


  • 0
    M
    public class Solution {
        public int firstUniqChar(String s) {
            int[] a = new int[26];
            for(int i=0; i<s.length(); i++){
                int index = s.charAt(i)-'a';
                if(a[index]==0) a[index] = i+1;
                else a[index] = -1;
            }
            int result = Integer.MAX_VALUE;
            for(int i=0;i<26;i++){
                if(a[i]!=0&&a[i]!=-1){
                    result = Math.min(result, a[i]-1);
                }
            }
            if(result==Integer.MAX_VALUE) return -1;
            return result;
        }
    }
    

Log in to reply
 

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