Java Solution beats 95.17% in 17ms


  • -1
    W
    public class Solution {
        public int firstUniqChar(String s) {
            char[] arr = s.toCharArray();
            if (arr.length == 1) return 0;
            int min = Integer.MAX_VALUE;
            int[] temp = new int[128];
            for (char c:arr) {
                temp[c]++;
            }
            for (int i= 0;i < temp.length;i++){
                if ( i == temp.length -1 && min == Integer.MAX_VALUE) return -1;
                if (temp[i] == 1)  min = Math.min(s.indexOf(i),min);
            }
            return min;
        }
    }

  • 1
    O

    every time you find a candidate min you search one time the original string. That's not efficient.


Log in to reply
 

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