본문 바로가기
Leetcode

[LeetCode] Valid Anagram 파이썬 (dict)

by YGSEO 2021. 4. 7.
728x90
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        def get_dict(word):
            res = {}
            for c in word:
                if c in res:
                    res[c] += 1
                else:
                    res[c] = 1
            return sorted(res.items())
            
        sd = get_dict(s)
        td = get_dict(t)
        return sd == td

dict를 sort 하려면 sorted 안에 dict.items()를 넣어주면 된다.

 

아래 처럼 key를 사용해서 key, value 순으로 sort가능하다.

ygseo.tistory.com/215

 

728x90

댓글