242. Valid Anagram

Given two strings s and , write a function to determine if t is an anagram of s.

Example 1:

 

 

Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

 
class Solution(object):
    def isAnagram(self, s, t):
        if not s and not t:            # reminder: always check the base case(s) 
            return True 
        if len(s) != len(t):           # reminder: always check the base case(s)
            return False 
        for char in set(s): 
            if s.count(char) != t.count(char):    # count has to run on O(n) time, therefore, 
                return False                      # I am not sure why this code runs faster? 
        return True                               # Less passes? 

 

以上

posted on 2018-08-24 13:07  jydd  阅读(58)  评论(0)    收藏  举报

导航