Simple 5 line O(n) java solution, no hashmap


  • 0
    C
    public int firstUniqChar(String s) {
            int[] map = new int[128];
            for(int i=0;i<s.length();i++) map[s.charAt(i)]++;
            for(int i=0;i<s.length();i++)
                if(map[s.charAt(i)] == 1) return i;
            return -1;
        }
    

Log in to reply
 

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