another java solution


  • 0
    R
        public int firstUniqChar(String s) {
            
            Map<Character, Integer> map = new LinkedHashMap();
            for(char c : s.toCharArray()) {
                if(map.containsKey(c)) {
                    map.put(c, map.get(c)+1);
                } else {
                    map.put(c, 1);
                }
            }
            char cc = '\0';
            
            for(Map.Entry<Character, Integer> es : map.entrySet()) {
                if(es.getValue()==1) {
                    cc=es.getKey();
                    break;
                }
            }
            System.out.println(cc);
            for(int i=0;i<s.length();i++) {
                if(s.charAt(i)==cc) {
                    return i;
                }
            }
            
            
            return -1;
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.