소프트웨어에 대한 모든 것

LeetCode 풀기 - 1941. Check if All Characters Have Equal Number of Occurrences 본문

알고리즘/LeetCode

LeetCode 풀기 - 1941. Check if All Characters Have Equal Number of Occurrences

앤테바 2021. 11. 6. 10:03
반응형

1941. Check if All Characters Have Equal Number of Occurrences

https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/

 

Check if All Characters Have Equal Number of Occurrences - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

문제)

솔루션1)

  • defaultdict() 사용해서 풀기
class Solution:
    def areOccurrencesEqual(self, s: str) -> bool:
        d = defaultdict(int)
        for c in s:
            d[c] += 1
        
        return len(set(d.values())) == 1

솔루션2)

  • 리스트 원소 갯수를 세는 Counter 객체를 사용해서 풀기
class Solution:
    def areOccurrencesEqual(self, s: str) -> bool:
        counter = Counter(s)
        return len(set(counter.values())) == 1

 

반응형
Comments