Lintcode 646. First Position Unique Character(Easy) (Python)

First Position Unique Character

Description

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Example
Given s = “lintcode”, return 0.

Given s = “lovelintcode”, return 2.

Code:

class Solution:
    """
    @param s: a string
    @return: it's index
    """
    def firstUniqChar(self, s):
        # write your code here
        res = collections.Counter()
        for i in s:
            res[i] += 1
        for i in range(len(s)):
            if res[s[i]] == 1:
                return i
        return -1

猜你喜欢

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