773. 有效的字母异位词

773. 有效的字母异位词

中文English

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

样例

样例 1:

输入: s = "anagram", t = "nagaram"
输出: true

样例 2:

输入: s = "rat", t = "car"
输出: false

挑战

如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?

注意事项

你可以假设字符串只包含小写字母。

class Solution:
    """
    @param s: string s
    @param t: string t
    @return: Given two strings s and t, write a function to determine if t is an anagram of s.
    """
    '''
    1.循环s,如果s里面的字符在t里面的话,就移除掉s和t对应的值,replace('','',1)
    2.如果不在里面的话,那么直接返回false,最后的时候判断s和t长度是否为0,则返回True,否则False
    '''
    def isAnagram(self,s,t):
        s = s + ' '
        while  True:
            pop_num = s[0]
            if pop_num == ' ':
                s = s.replace(' ','',1)
                break
            if pop_num not in t:
                return False
            ##否则的话,分别移除掉相同的字符
            s = s.replace(pop_num,'',1)
            t = t.replace(pop_num,'',1)
        if len(s) == 0 and len(t) == 0:
            return True
        return False

原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12550773.html