<div>
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
<pre>Input: s = "leetcode" Output: 0 </pre>
Example 2:
<pre>Input: s = "loveleetcode" Output: 2 </pre>
Example 3:
<pre>Input: s = "aabb" Output: -1 </pre>
Constraints:
1 <= s.length <= 10<sup>5</sup>sconsists of only lowercase English letters.
</div>
题目大意:
求字符串中第一个唯一的字符下标
解题思路:
Easy题。先统计频率,然后再遍历一次字符串找到频率为1的字符
解题步骤:
N/A
注意事项:
Python代码:
1
2
3
4
5
6def firstUniqChar(self, s: str) -> int:
char_to_count = collections.Counter(s)
for i in range(len(s)):
if char_to_count[s[i]] == 1:
return i
return -1
算法分析:
时间复杂度为O(n),空间复杂度O(1), 26个字母为常量空间


