Byte by Byte

python - string Module ) from collections import Counter 본문

개발 로그/알고리즘

python - string Module ) from collections import Counter

CyberSoak 2022. 5. 11. 00:28

from collections import Counter

 

str에 대해서, 각 char를 key로, 발생 빈도 수를 value를 하는 dict를 만들어줌.

str = "hello"

Counter(str) 하면 , { 'h' : 1 , 'e' : 1 , 'l' : 2, 'o' : 1 }

이렇게 나옴.

 

dict for loop 는

for key, value in {dict}.items():

 

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        
        from collections import Counter
        rans = Counter(ransomNote)
        magz = Counter(magazine)
        
        for key,value in rans.items():
            rans[key] = rans[key]-magz[key]
        for key,value in rans.items():
            if rans[key] > 0:
                return False
        return True

'개발 로그 > 알고리즘' 카테고리의 다른 글

442. Find All Duplicates in an Array.cpp  (0) 2022.01.24
1854. Maximum Population Year.cpp  (0) 2021.12.17
49. Group Anagrams.cpp  (0) 2021.12.16
1078. Occurrences After Bigram.cpp  (0) 2021.12.15
1598. Crawler Log Folder.cpp  (0) 2021.12.15