LeetCode - 387. First Unique Character in a String

xiaoxiao2021-02-28  105

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

s = "leetcode" return 0. s = "loveleetcode", return 2.

Note: You may assume the string contain only lowercase letters.

A:

class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ minIndex = len(s) charList = [ch for ch in set(s) if s.count(ch) == 1] if len(charList) == 0: return -1 for ch in charList: if s.index(ch) < minIndex: minIndex = s.index(ch) return minIndex
转载请注明原文地址: https://www.6miu.com/read-54385.html

最新回复(0)