본문 바로가기

IT/코딩테스트

[Leetcode] 1002. Find Common Characters

728x90

https://leetcode.com/problems/find-common-characters/

 

LeetCode - The World's Leading Online Programming Learning Platform

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

class Solution:
    def commonChars(self, words: List[str]) -> List[str]:
        result = []
        result_dic = {}

        for c in words[0]:
            if result_dic.get(c) is None:
                result_dic[c] = 1
            else:
                result_dic[c] += 1

        for word in words[1:]:
            for key, value in result_dic.copy().items():
                if word.count(key) == 0:
                    result_dic.pop(key)
                elif word.count(key) < value:
                    result_dic[key] = word.count(key)
        
        for key, value in result_dic.items():
            for i in range(0, value):
                result.append(key)

        return result
728x90