0387. First Unique Character in a String

387. First Unique Character in a String #

题目 #

给定一个字符串 s,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1

思路 #

代码 #

class Solution {
    public int firstUniqChar(String s) {
        int[] seen = new int[26];
        for (int i = 0; i < s.length(); i++) seen[s.charAt(i) - 'a'] += 1;
        for (int i = 0; i < s.length(); i++) if (seen[s.charAt(i) - 'a'] == 1) return i;
        return -1;
    }
}