
class Solution {
// 给定一个字符串 s ,找到 它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1
public int firstUniqChar(String s) {
int size = s.length();
//存取每个数字的出现次数用哈希表
//{c=1, t=1, d=1, e=3, l=1, o=1}
//hashmap存元素是无序的吗 -> yes无序的
HashMap<Character,Integer> map = new HashMap<>();
for(char c:s.toCharArray()){
map.put(c,map.getOrDefault(c,0)+1);
}
System.out.println(map);
for(int i = 0;i < s.length();i++){
if(map.get(s.charAt(i)) == 1){//s.charAt(i))这个是关键
return i;
}
}
return -1;
}
}