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.
classSolution:defcommonChars(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