Description
Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.
Example
1 | s = "leetcode" |
Solution
1 | class Solution { |
Method 2: More faster1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Solution {
public int firstUniqChar(String s) {
if (s.length() == 0) return -1;
int[] countRepeats = new int[128];
for(int i=0; i<s.length(); i++){
countRepeats[s.charAt(i)]++;
}
for(int i=0; i<s.length(); i++){
if(countRepeats[s.charAt(i)] == 1)
return i;
}
return -1;
}
}