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.
思路,统计每个每个字母出现的次数到一个数组中,数组大小为26个小写字母。
int firstUniqChar(char* s) {
int i;
char *str;
int count[26];
memset(count, 0, 26 * sizeof(int));
for(str = s; *str != '\0'; str++) {
i = *str - 'a';
count[i]++;
}
for(str = s; *str != '\0'; str++) {
i = *str - 'a';
if(count[i] == 1)
return str - s;
}
return -1;
}