1657. Determine if Two Strings Are Close

1657. Determine if Two Strings Are Close

class Solution:
    def closeStrings(self, word1: str, word2: str) -> bool:
        mydict=defaultdict(int)
        if len(word1)!=len(word2):return False
        for a in word1:
            mydict[a]+=1
        mydict2=defaultdict(int)
        for key in mydict:
            mydict2[mydict[key]]+=1
        mydict=defaultdict(int)
        for a in word2:
            mydict[a]+=1
        for key in mydict:
            mydict2[mydict[key]]-=1
        
        for key in mydict2:
            if mydict2[key]!=0:return False

        return True and set(word1)==set(word2)

两个集合的初始元素也要相同

你可能感兴趣的:(leetcode)