给定一个字符串 s
,找到 它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1
。
要求:
1 <= s.length <= 105
s
只包含小写字母-
int firstUniqChar(char * s){ int len = strlen(s); int cout[26] = {0}; for (int i = 0; i < len; ++i) { cout[s[i] - 'a']++; } for (int i = 0; i < len; ++i) { if (cout[s[i]-'a'] == 1) { return i; } } return -1; }