1002. Find Common Characters - LeetCode Solution


String Hash Table Array

Python Code:

class Solution:
    def commonChars(self, words: List[str]) -> List[str]:
        actual_dict = {}
        for i in range(len(words[0])):
            if words[0][i] in actual_dict:
                actual_dict[words[0][i]]+=1
            else:
                actual_dict[words[0][i]]=1
        
        for i in range(1, len(words), 1):
            
            another = {}
            for j in range(len(words[i])):
                if words[i][j] in another:
                    another[words[i][j]] +=1
                else:
                    another[words[i][j]] = 1
            
            for j in actual_dict:
                if j in another:
                    if another[j] <= actual_dict[j]:
                        actual_dict[j] = another[j]
                else:
                    actual_dict[j] = -1
        ans = []
        for i in actual_dict:
            if actual_dict[i] == -1:
                continue
            else:
                ans+= [i] * actual_dict[i]
        return ans


Comments

Submit
0 Comments
More Questions

729D - Sea Battle
788A - Functions again
1245B - Restricted RPS
1490D - Permutation Transformation
1087B - Div Times Mod
1213B - Bad Prices
1726B - Mainak and Interesting Sequence
1726D - Edge Split
1726C - Jatayu's Balanced Bracket Sequence
1726A - Mainak and Array
1613C - Poisoned Dagger
475B - Strongly Connected City
652B - z-sort
124B - Permutations
1496C - Diamond Miner
680B - Bear and Finding Criminals
1036E - Covered Points
1015D - Walking Between Houses
155B - Combination
1531A - Зингер | color
1678A - Tokitsukaze and All Zero Sequence
896A - Nephren gives a riddle
761A - Dasha and Stairs
1728B - Best Permutation
1728A - Colored Balls Revisited
276B - Little Girl and Game
1181A - Chunga-Changa
1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme