Last active
October 15, 2017 03:44
-
-
Save cixuuz/c4c29fa5348d56d301d338810f07d1a6 to your computer and use it in GitHub Desktop.
[387. First Unique Character in a String] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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