class Solution { public int firstUniqChar(String s) { int len = s.length(); int[] res = new int[26]; for(int i=0;i<len;i++) { res[s.charAt(i)-'a']++; } for(int j=0;j<len;j++) { if(res[s.charAt(j)-'a'] == 1) return j; } 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