剑指Offer-50 字符串中第一个只出现一次的字符

题目:

在字符串中找出第一个只出现一次的字符。如输入"abaccdeff",则输出b。如果字符串中不存在只出现一次的字符,返回#字符。

解答:

class Solution:
    def firstNotRepeatingChar(self, s):
        """
        :type s: str
        :rtype: str
        """
        d = {}
        for ind, sym in enumerate(s):
            if sym not in d:
                d[sym] = [ind]
            else:
                d[sym].append(ind)
        minind = len(s)
        ch = None
        for i in d:
            if(len(d[i]) == 1 and d[i][0] < minind):
                minind = d[i][0]
                ch = i
        return ch if ch else '#'    

猜你喜欢

转载自blog.csdn.net/u013796880/article/details/84845405