Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Last active October 15, 2017 03:44
Show Gist options
  • Save cixuuz/c4c29fa5348d56d301d338810f07d1a6 to your computer and use it in GitHub Desktop.
Save cixuuz/c4c29fa5348d56d301d338810f07d1a6 to your computer and use it in GitHub Desktop.
[387. First Unique Character in a String] #leetcode
# use dictionary
import collections
class Solution(object):
# O(n)
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
counts = collections.Counter(s)
for i, c in enumerate(s):
if counts[c] == 1:
return i
return -1
# use string count
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
letters='abcdefghijklmnopqrstuvwxyz'
index=[s.index(l) for l in letters if s.count(l) == 1]
return min(index) if len(index) > 0 else -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment