Easy 20ms Java solution


  • 0
    A
    public class Solution {
        public int firstUniqChar(String s) {
            int[] chars = new int[26];
            char[] schars = s.toCharArray();//if we do not use char array time will be 30ms
            for (int i = 0; i < schars.length; i++) {
                chars[schars[i] - 'a']++;
            }
            for (int i = 0; i < schars.length; i++) {
                if (chars[schars[i] - 'a'] == 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.