leetcode--python--1002

1002. Find common characters

Given a string array A consisting of only lowercase letters, return a list of all characters (including repeated characters) displayed in each string in the list. For example, if a character appears 3 times in each string, but not 4 times, you need to include the character 3 times in the final answer.

You can return the answers in any order.

There are two ways of writing, one is traversed by set and the other is traversed by list. Where is the difference between the two

class Solution(object):
    def commonChars(self, A):

        """
        :type A: List[str]
        :rtype: List[str]
        """
        key = A[0]
        result = []
        for i in set(key):
            num = min([k.count(i) for k in A])
            result += [i]*num
        return(result)

Insert picture description here

class Solution(object):
    def commonChars(self, A):

        """
        :type A: List[str]
        :rtype: List[str]
        """
        key = A[0]
        result = []
        for i in list(set(key)):
            num = min([k.count(i) for k in A])
            result += [i]*num
        return(result)

Insert picture description here

Guess you like

Origin blog.csdn.net/AWhiteDongDong/article/details/110384536