class Solution {
public:
vector<string> commonChars(vector<string>& words) {
}
};
1002. 查找共用字符
给你一个字符串数组words
,请你找出所有在 words
的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序 返回答案。
示例 1:
输入:words = ["bella","label","roller"] 输出:["e","l","l"]
示例 2:
输入:words = ["cool","lock","cook"] 输出:["c","o"]
提示:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i]
由小写英文字母组成相似题目
原站题解
golang 解法, 执行用时: 0 ms, 内存消耗: 2.8 MB, 提交时间: 2021-06-10 14:41:29
func commonChars(words []string) (ans []string) { minFreq := [26]int{} for i := range minFreq { minFreq[i] = math.MaxInt64 } for _, word := range words { freq := [26]int{} for _, b := range word { freq[b-'a']++ } for i, f := range freq[:] { if f < minFreq[i] { minFreq[i] = f } } } for i := byte(0); i < 26; i++ { for j := 0; j < minFreq[i]; j++ { ans = append(ans, string('a'+i)) } } return }