Find the first non-repeated character in a string

前端 未结 21 1581
有刺的猬
有刺的猬 2020-12-06 03:53

I read of a job interview question to write some code for the following:

Write an efficient function to find the first nonrepeated character in a st

21条回答
  •  误落风尘
    2020-12-06 04:31

    More elegant way

    def NotRepeatingCharacter(s):
        for c in s:
            if s.find(c) == s.rfind(c):
                return c
        return '_'
    

提交回复
热议问题