Best Java one pass O(n) solution


  • 0
    A

    public class Solution {
    public int firstUniqChar(String s) {

        int[] arr = new int[26];
        if(s == null || s.length() == 0) {
            return -1;
        }
        int n = s.length();
        int j = 0;
        for(int i = 0;i < n;i++) {
            while(j < n) {
                if(arr[s.charAt(j) - 'a']++ == 0 || arr[s.charAt(i) - 'a'] < 2) {
                    j++;
                } else {
                    break;
                }
            }
            if(j == n) {
                return i;
            }
        }
        return -1;
    }
    

    }


Log in to reply
 

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