class Solution {
public int firstUniqChar(String s) {
HashMap<Character,Integer> map = new HashMap<>();
for(char ch : s.toCharArray())
map.put(ch , map.getOrDefault(ch,0)+1);
for(int i = 0 ; i < s.length() ;i++){
char ch = s.charAt(i);
if(map.get(ch)==1) return i;
}
return -1;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter