【LEETCODE】242-Valid Anagram [Python]

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

For example,

s = "anagram", t = "nagaram", return true.

s = "rat", t = "car", return false.

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?



Python:

class Solution:
    # @param {string} s
    # @param {string} t
    # @return {boolean}
    def isAnagram(self, s, t):
        
        if len(s)==0 and len(t)==0:

            return True


        if len(s.split())==0 or len(t.split())==0:

            return False

        else:

            c1 = self.statics(s)
            c2 = self.statics(t)

            if c1==c2:

                return True

            else:

                return False


    def statics(self,s):

        count = [0]*26


        for j in range(ord('a'),(ord('z')+1)):

            count[j-97] = s.count(chr(j))

        return count


你可能感兴趣的:(LeetCode,python)