LeetCode 1002. Find common characters

LeetCode 1002. Find common characters

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Topic :
    Given a string array consisting of only lowercase letters A, 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.
  • Example :
示例 1 :
输入:["bella","label","roller"]
输出:["e","l","l"]
示例 2 :
输入:["cool","lock","cook"]
输出:["c","o"]
  • Tips :
    1. 1 <= A.length <= 100
    2. 1 <= A[i].length <= 100
    3. A[i][j] Are lowercase letters
  • Code:
class Solution:
    def commonChars(self, A: List[str]) -> List[str]:
        result, temp = [], set(A[0])
        for i in temp:
            result += min(a.count(i) for a in A) * i
        return result
# 执行用时 :52 ms, 在所有 Python3 提交中击败了84.84%的用户
# 内存消耗 :13.9 MB, 在所有 Python3 提交中击败了100.00%的用户
  • Algorithm description:
    According to the requirements of the title, find the intersection of the number of characters between each string; if it Ais an empty set, do not execute the forloop; use the function to remove the repeated Aelements A[0]from the first element in the middle to obtain the middle element the other elements in the minimum number, and then adding the minimum number of elements returned .set()temptempiAresultiresult

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/106722643