Characters that appear only once for the first time (Java)
Title: Find the first character that appears only once in a string (0 <= string length <= 10000, all composed of letters), and return its position, if not, return -1 (case-sensitive ).
Code:
import java.util.HashMap;
import java.util.Map;
/**
* @desc 第一次只出现一次的字符
* @autho zhaoliang
* @date 20200415
*/
public class Main50_ {
//在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,
// 并返回它的位置, 如果没有则返回 -1(需要区分大小写).
public int FirstNotRepeatingChar(String str) {
Map<Character,Integer> map =new HashMap<>();
for (int i = 0;i<str.length();i++){
map.put(str.charAt(i),map.getOrDefault(str.charAt(i),0)+1);
}
for(int i = 0;i<str.length();i++){
if (map.get(str.charAt(i))==1){
return i;
}
}
return -1;
}
}