在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = “abaccdeff”
返回 “b”
s = “”
返回 " "
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# 用i遍历去重后的s,若i的出现次数不为1则将i存入exists,再次用i遍历s,若i不存在于exists则返回i,否则返回" "
class Solution:
def firstUniqChar(self, s: str) -> str:
new_s = set(s)
exists = []
for i in new_s:
if s.count(i) != 1:
exists.append(i)
for i in s:
if i not in exists:
return i
return " "