Java Solution using 2 sets.


  • 0
    S
    public class Solution {
        public int firstUniqChar(String s) {
            Set<Character> set = new HashSet();
            Set<Character> dup = new HashSet();
            
            for (int i = 0; i < s.length(); i++) {
                if (!set.add(s.charAt(i))) {
                    dup.add(s.charAt(i));
                }
            }
            
            for (int i = 0; i < s.length(); i++) {
                if (!dup.contains(s.charAt(i))) {
                    return i;
                }
            }
            return -1;
        }
    }
    

Log in to reply
 

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