package offer;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* offer interview 35
*/
public class Test35 {
public static char firstNotRepeatingChar(String s) {
if (s == null || s.length() < 1) {
throw new IllegalArgumentException("Arg should not be null or empty");
}
Map<Character, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
map.put(c, -2);
} else {
map.put(c, i);
}
}
Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
int idx = Integer.MAX_VALUE;
char result = '\0';
for (Map.Entry<Character, Integer> entry : entrySet) {
if (entry.getValue() >= 0 && entry.getValue() < idx) {
idx = entry.getValue();
result = entry.getKey();
}
}
return result;
}
public static void main(String[] args){
System.out.println(firstNotRepeatingChar("google"));
System.out.println(firstNotRepeatingChar("aabccdbd")); // '\0'
System.out.println(firstNotRepeatingChar("abcdefg")); // a
System.out.println(firstNotRepeatingChar("gfedcba")); // g
System.out.println(firstNotRepeatingChar("zgfedcba")); // z
}
}
========
欢迎关注群聊: