The first character that only appears once in "Sword Finger Offer"

The first character that only appears once in "Sword Finger Offer"

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Topic :
    Find the first character that appears only once in a string (0<=string length<=10000, all composed of letters), and return its position, if not, return -1 (case sensitive ). (Counting from 0).
  • Example :
示例 1 :
输入:"google"
返回值:4
  • Code 1:
# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        for i in range(len(s)):
            if s.count(s[i]) == 1:
                return i
        return -1
  • Algorithm description:
    Traverse the string, count the number of occurrences of each element, and return the index of the character that appears only once.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/114985699