1. 程式人生 > >python leetcode 387. First Unique Character in a String

python leetcode 387. First Unique Character in a String

利用find函式能極大地減少執行時間

class Solution:
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        c='qwertyuiopasdfghjklzxcvbnm'
        res=2**31-1
        for n in c:
            index = s.find(n)
            if index ==-1:
                continue
            if s[index+1:].find(n)==-1:
                res=min(res,index)
        return res if res!=2**31-1 else -1