>[success] # 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] 由小寫英文字母組成
來源:力扣(LeetCode)
鏈接:https://leetcode.cn/problems/find-common-characters
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
>[danger] ##### 第一次解題思路
用**Map** 作為記錄字符串出現次數,對比出現次數最小值即為出現的**共用字符**
舉個例子 ["bella","zabel","roller"]


~~~
var commonChars = function (words) {
// 用map 計算出第一組每個字母個數
const minMap = {}
const [firstGroup, ...otherGroup] = words
for (let w of firstGroup) {
if (!minMap[w]) {
minMap[w] = 0
}
minMap[w]++
}
// 計算其他組單詞出現頻率
otherGroup.forEach((word) => {
const currentMap = {}
for (let w of word) {
if (minMap[w] && !currentMap[w]) {
currentMap[w] = 0
}
currentMap[w]++
}
// 比較最少次數
for (let k in minMap) {
// 跟新最少出現次數頻率
minMap[k] = Math.min(minMap[k], currentMap[k] || 0)
}
})
const res = []
for (let k in minMap) {
let count = minMap[k]
while (count) {
res.push(k)
count--
}
}
return res
}
~~~
>[info] ## 題解
題中說`由小寫英文字母組成` 因此只能有**26**個單詞,利用固定26這個條件可以用**數組形式**,創建一個長度為26的數組,將字母找到對應數組位置可利用**ASCII**碼,當想獲取對應單詞時候可以用**ASCII**反解析映射對應的字母

將每組收集,對比每項最小值,即可得到**共用字符**

>[danger] ##### js 題解
~~~
var commonChars = function (words) {
// 將第一組值映射記錄到對應數組上
const [firstGroup, ...otherGroup] = words
// 創建長度26的數組保存對應 字母
const minWordCountsLs = new Array(26).fill(0)
for (let w of firstGroup) {
// 都是小寫字母 為了找到屬于數組對應映射位置使用,a的ASCII 碼為97
// 因此 減掉97 例如 a 原本應該保存數組0的位置 97-97 =0
const idx = w.charCodeAt(0) - 97
++minWordCountsLs[idx]
}
// 依次比較找到 相互直接最小的出現次數替換
for (let other of otherGroup) {
// 計算出當前組字符出現次數
const currentCountsLs = new Array(26).fill(0)
for (let w of other) {
const idx = w.charCodeAt(0) - 97
++currentCountsLs[idx]
}
// 循環比較更新最小出現值數組
minWordCountsLs.forEach((item, index) => {
minWordCountsLs[index] = Math.min(item, currentCountsLs[index])
})
}
// 將出現此次字母記錄
return minWordCountsLs.reduce((acc, count, index) => {
while (count) {
acc.push(String.fromCharCode(index + 97))
count--
}
return acc
}, [])
}
~~~
>[danger] ##### java
~~~
import java.util.List;
import java.util.ArrayList;
public class Solution {
public List<String> commonChars(String[] words) {
// 創建一個26長度數組
int[] minLs = new int[26];
// 循環第一組數組記錄到數組中
for(char c:words[0].toCharArray()){
// 減掉a從0開始可以映射對應數組
int idx = c -'a';
minLs[idx]++;
}
// 依次尋其他項找到最小數組
for(int i=1;i< words.length;i++){
// 記錄數組長度
int[] currentLs = new int[26];
// 循環字符串
for(char c:words[i].toCharArray()){
int idx = c -'a';
currentLs[idx]++;
}
// 循環找到相交出現最小次數
for(int j=0;j<26;j++){
minLs[j] = Math.min(minLs[j],currentLs[j]);
}
}
// 求出對應字母
List<String> wLs = new ArrayList<>();
for(int i=0;i<26;i++){
int counts = minLs[i];
while (counts>0){
wLs.add(String.valueOf((char)(i+'a')));
counts--;
}
}
return wLs;
}
}
~~~
>[info] ## 其他解題思路
兩個字符進行比較,如果有相同的則記錄下來,但要注意需要將已經比較后的值去處,否則會出現重復比較

~~~
var commonChars = function (words) {
return words.reduce((acc, cur) => {
return [...acc].filter((item) => {
const flag = cur.includes(item)
cur = cur.replace(item, '')
return flag
})
})
}
~~~
- 刷題準備
- 統計數組中元素出現次數
- Leetcode -- 442數組中重復的數據
- leetcode -- 448 找到所有數組中消失的數字
- 字符類似題
- Leetcode -- 1002 查找共用字符
- Leetcode -- 1370上升下降字符串
- 指針類題解
- Leetcode -- 283 移動零
- Leetcode -- 26. 刪除有序數組中的重復項
- Leetcode -- 80. 刪除有序數組中的重復項 II
- Leetcode -- 27. 移除元素
- Leetcode -- 344. 反轉字符串
- Leetcode -- 125 驗證回文串
- Leetcode -- 11 盛最多水的容器
- Leetcode -- 1480. 一維數組的動態和