Minimum Characters For Words

Tiffany ·2024년 3월 12일
0

AlgoExpert

목록 보기
16/20

모든 단어의 캐릭터에 대한 로컬해시를 만들고 로컬해시의 가치값을 이용해 글로벌 해시를 업데이트 한다.

from collections import defaultdict 
def minimumCharactersForWords(words):
    mapGlobal = {}
    result = []
    for word in words:
        mapLocal = defaultdict(int)
        for char in word:
            mapLocal[char] += 1  
            #concurrrently init and update maxiumum for global dict 
        for letter, localVal in mapLocal.items():
            mapGlobal[letter] = max(mapGlobal.get(letter, 0), localVal)
            
    for key, value in mapGlobal.items():
        while value > 0:
            result.append(key) # print keys 
            value -= 1 
    return result 

글로벌 해시를 돌면서 가치값이 있다면, 해당하는 키를 결과 어레이에 append 하므로 hash.keys() 가 아닌 hash.items() 이라는것.

profile
Love what you do and don't quit.

0개의 댓글