387. 字符串中的第一个唯一字符
class Solution {
public int firstUniqChar(String s) {
int[] arr = new int[26];
/**
* 先将所有字母的词频保存下来
*/
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 'a']++;
}
/**
* 然后挨个进行判断是否等于1
*/
for (int i = 0; i < s.length(); i++){
if (arr[s.charAt(i) - 'a'] == 1){
return i;
}
}
return -1;
}
}
https://leetcode-cn.com/problems/first-unique-character-in-a-string/