leetcode 242. Valid Anagram

来源:互联网 发布:河北工龄工资算法 编辑:程序博客网 时间:2024/06/14 13:23

Python leetcode

242. Valid Anagram


242. Valid Anagram

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?




猜谜语,就是判断一个字符串的的所有元素和另一个字符串所有元素一样,只是顺序不一样而已


#coding=utf-8
class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        #ss = sorted(list(s))
        ss = sorted(s)
        print ss
        #tt = sorted(list(t))
        tt = sorted(t)
        print tt
        return ss==tt

    def isAnagram1(self,s,t):
        sdict={};tdict={}
        for item in s:
            sdict[item]=sdict.get(item,0)+1
        for item in t:
            tdict[item]=tdict.get(item,0)+1
        return sdict==tdict

S=Solution()
s='anagram';t='nagaram'
print S.isAnagram1(s,t)


0 0
原创粉丝点击