Find the first un-repeated character in a string

后端 未结 30 1270
猫巷女王i
猫巷女王i 2020-11-27 18:29

What is the quickest way to find the first character which only appears once in a string?

30条回答
  •  自闭症患者
    2020-11-27 19:18

    def first_non_repeated_character(string):
      chars = []
      repeated = []
      for character in string:
        if character in repeated:
            ... discard it.
        else if character in chars:
          chars.remove(character)
          repeated.append(character)
        else:
          if not character in repeated:
            chars.append(character)
      if len(chars):
        return chars[0]
      else:
        return False
    

提交回复
热议问题