LeetCode/Grind169

242. Valid Anagram

hyunkookim 2025. 4. 22. 03:39

 

 

242. Valid Anagram

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        # t 가 s의 anagram 이면 return True
        # An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
        # 문자와 각 문자의 개수가 같으면 anagram

        def getStrCountChar(strs):
            countChar = {}
            for s in strs:
                if s not in countChar:
                    countChar[s]=1
                else:
                    countChar[s]+=1

            countChar = dict(sorted(countChar.items()))
            return "".join([f"{k}#{v}_" for k, v in countChar.items()])

        # print(getStrCountChar(s))
        # print(getStrCountChar(t))
        return getStrCountChar(s) == getStrCountChar(t)
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        # anagram 이 되려면, 문자 종류와 개수까지 같으면
        return Counter(s) == Counter(t)

'LeetCode > Grind169' 카테고리의 다른 글

733. Flood Fill  (1) 2025.04.22
704. Binary Search  (0) 2025.04.22
226. Invert Binary Tree  (0) 2025.04.22
125. Valid Palindrome  (0) 2025.04.22
121. Best Time to Buy and Sell Stock ☆  (0) 2025.04.22