242. valid anagram
给定两个字符串,问是否是 anagram(同字母异序)
python 的做法很简单,一行就好了
def isAnagram(self, s, t):
return sorted(s) == sorted(t)
这个是时间复杂度是 O(nlogn)
想要提高 速度的话 可以用 hash table
用两个list 存每个字符出现次数,然后比较。
时间复杂度是O(n), 但是空间复杂度也是O(n)
387: first unique character in a string
给一个string, 然后问string中只出现一次的 字符的最小的下标
首先想到的是字典:
def firstUniqChar(self, s):
Dic = {}
for char in s:
if char in Dic:
Dic[char] += 1
else:
Dic[char] = 1
for i in xrange(len(s)):
if Dic[s[i]] == 1:
return i
return -1
然后discussion里面有个更快的方法:
def firstUniqChar(self, s):
"""
:type s: str
:rtype