> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-28-2022-387.md).

# 01/28/2022 387

Unicode does NOT have a million characters; it's under 140,000. And most of those are NOT lower-case letters. How is anyone making an assumption when the problem explicitly states that it's only lower-case letters? And whether it's 26, or 100, or 140K, or even 1M, it's still a constant. The number of possible lower-case letters never depends on the length of the input string, so why would you ever claim O(n)?

Use hashmap to input each charater and its counts. Then in for loop, to check the the first count of character is 1.&#x20;

Time O(N)

Space O(1)

```
public class Solution {
    public int firstUniqChar(String s) {
        if (s == null || s.isEmpty()) {
            return -1;
        }
        int[] letters = new int[26];
        for (int i = 0; i < s.length(); i++) {
            letters[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < s.length(); i++) {
            if (letters[s.charAt(i) - 'a'] == 1) {
                return i;
            }
        }
        return -1;
    }
}
```

method 2:

create a table with size 26 for the lowercase English Letters. Loop through the String, convert char to int and increment its count. Then finally loop through the table and found the first one with the value of 1.

Time O(N)

Space O(1)

```
public int firstUniqChar(String s) {
        for(char c : s.toCharArray()){
            int index = s.indexOf(c);
            int lastIndex = s.lastIndexOf(c);
            if(index == lastIndex)
                return index;
        }
        return -1;
    }
//runtime O(n^2)
```

```
public class Solution {
    public int firstUniqChar(String s) {
        if (s==null || s.length()==0) return -1;
        int len = s.length();
        if (len==1) return 0;
        char[] cc = s.toCharArray();
        int slow =0, fast=1;
        int[] count = new int[256];
        count[cc[slow]]++;
        while (fast < len) {
            count[cc[fast]]++;
            // if slow pointer is not a unique character anymore, move to the next unique one
            while (slow < len && count[cc[slow]] > 1) slow++;  
            if (slow >= len) return -1; // no unique character exist
            if (count[cc[slow]]==0) { // not yet visited by the fast pointer
                count[cc[slow]]++; 
                fast=slow; // reset the fast pointer
            }
            fast++;
        }
        return slow;
    }
}
```

```
class Solution {
    public int firstUniqChar(String s) {
        // for empty string no unique char
        if (s == null || s.length() == 0) return -1;
        
        // since s contains only lowercase letters, there are only 26 possible
        // we can keep count of chars encountered
        int[] count = new int[26];
        
        // use slow and fast pointer, fast to incrementally read char
        // slow to increment if found duplicate character. Slow points to unique char
        int slow = 0;
        int fast = 1;

        // for first char count is 1
        count[s.charAt(slow)-'a']++;
        
        // iterate over all chars starting from second char to check if dupe
        while (fast < s.length()) {
            
            count[s.charAt(fast)-'a']++;
            
            // if found a dupe char, increment slow to next unique char
            while (slow < s.length() && count[s.charAt(slow)-'a'] > 1) slow++;
            
            // if all chars are dupe, return
            if (slow == s.length()) return -1;

            // if this char is covered first time, reset fast to 1 char ahead
            if (count[s.charAt(slow)-'a'] == 0) {
                fast = slow+1;
                count[s.charAt(slow)-'a']++;
            }
            // else increment fast to next char
            else {
                fast++;
            }
        }
        return slow;
    }
}
```

```
class Solution:
    def firstUniqChar(self, s: str) -> int:
        if len(s) == 1:
            return 0
        slow, fast = 0, 1
        count = dict()
        count[s[slow]] = count.get(s[slow],0) + 1
        while (fast < len(s)):
            count[s[fast]] = count.get(s[fast],0) + 1
            while (slow < len(s) and count.get(s[slow], 0) > 1):
                slow += 1
            if slow >= len(s):
                return -1
            fast += 1
        return slow
```
