Lintcode 209. First Unique Character in a String (Easy) (python)

209. First Unique Character in a String

Description:

Find the first unique character in a given string. You can assume that there is at least one unique character in the string.

Example
For “abaccdeff”, return ‘b’.

Code:

class Solution:
    """
    @param str: str: the given string
    @return: char: the first unique character in a given string
    """
    def firstUniqChar(self, str):
        # Write your code here
        dict = {}
        for i in str:
            dict[i] = dict[i]+1 if i in dict else 1
        for i in range(len(str)):
            if dict[str[i]] == 1:
                return str[i]

猜你喜欢

转载自blog.csdn.net/weixin_41677877/article/details/80946327
今日推荐