1002. Find Common Characters
Given an array A
of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
You may return the answer in any order.
Example 1:
1 | Input: ["bella","label","roller"] |
Example 2:
1 | Input: ["cool","lock","cook"] |
Note:
1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j]
is a lowercase letter
Solution:
Approach One : 先计算出A[0]字符每个字符出现的次数,然后到其余字符串去匹配,min_times为该字符在各字符串出现的最小次数, min_times = min(min_times,count(temp,A[k]));最小为0,即存在一个字符串不含有该字符,
最终min_times的取值就是该字符要放入result中的次数,解决字符重复出现的问题。
- Time Complexity :O(n^2)
- Space Complexity: O(n)
1 | int (char c,string str)大专栏 Leetcode-Day Threean> |