import java.util.HashMap;
import java.util.Map;
// 用HashMap保存,顺序扫描str,第一个为1的就是结果
public class Solution {
public int FirstNotRepeatingChar(String str) {
char[] chars = str.toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (char c : chars) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
int i = 0;
for (char c : chars) {
if (map.get(c) == 1) {
return i;
}
i++;
}
return -1;
}
} 
京公网安备 11010502036488号