Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode", return 2.
Note: You may assume the string contain only lowercase letters.
Subscribe to see which companies asked this question.
public class Solution {
public int firstUniqChar(String s) {
if (s == null) {
return -1;
}
int length = s.length();
if (length < 2) {
return length -1;
}
char[] array = s.toCharArray();
for (int i = 0; i < length; i++) {
boolean b = true;
if (array [i] == '0') {
continue;
}
for (int j = i + 1; j < length; j++) {
if (array[i] == array[j]) {
b = false;
array[j] = '0';
}
}
if (b) {
return i;
}
}
return -1;
}
}