Skip to content

Commit a7eebca

Browse files
authored
feat: update solutions to lc problem: No.1002 (doocs#947)
No.1002.Find Common Characters
1 parent 988163c commit a7eebca

File tree

6 files changed

+159
-150
lines changed

6 files changed

+159
-150
lines changed

solution/1000-1099/1002.Find Common Characters/README.md

+57-50
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
**方法一:计数**
42+
43+
我们用一个长度为 $26$ 的数组 $cnt$ 记录每个字符在所有字符串中出现的最小次数,最后遍历 $cnt$ 数组,将出现次数大于 $0$ 的字符加入答案即可。
44+
45+
时间复杂度 $O(n \sum w_i)$,空间复杂度 $O(C)$。其中 $n$ 为字符串数组 $words$ 的长度,而 $w_i$ 为字符串数组 $words$ 中第 $i$ 个字符串的长度,另外 $C$ 为字符集的大小,本题中 $C = 26$。
46+
4147
<!-- tabs:start -->
4248

4349
### **Python3**
@@ -47,18 +53,15 @@
4753
```python
4854
class Solution:
4955
def commonChars(self, words: List[str]) -> List[str]:
50-
freq = [10000] * 26
51-
for word in words:
52-
t = [0] * 26
53-
for c in word:
54-
t[ord(c) - ord('a')] += 1
55-
for i in range(26):
56-
freq[i] = min(freq[i], t[i])
57-
res = []
58-
for i in range(26):
59-
if freq[i] > 0:
60-
res.extend([chr(i + ord("a"))] * freq[i])
61-
return res
56+
cnt = Counter(words[0])
57+
for w in words:
58+
ccnt = Counter(w)
59+
for c in cnt.keys():
60+
cnt[c] = min(cnt[c], ccnt[c])
61+
ans = []
62+
for c, v in cnt.items():
63+
ans.extend([c] * v)
64+
return ans
6265
```
6366

6467
### **Java**
@@ -68,24 +71,24 @@ class Solution:
6871
```java
6972
class Solution {
7073
public List<String> commonChars(String[] words) {
71-
int[] freq = new int[26];
72-
Arrays.fill(freq, 10000);
73-
for (String word : words) {
74-
int[] t = new int[26];
75-
for (char c : word.toCharArray()) {
76-
++t[c - 'a'];
74+
int[] cnt = new int[26];
75+
Arrays.fill(cnt, 10000);
76+
for (String w : words) {
77+
int[] ccnt = new int[26];
78+
for (int i = 0; i < w.length(); ++i) {
79+
++ccnt[w.charAt(i) - 'a'];
7780
}
7881
for (int i = 0; i < 26; ++i) {
79-
freq[i] = Math.min(freq[i], t[i]);
82+
cnt[i] = Math.min(cnt[i], ccnt[i]);
8083
}
8184
}
82-
List<String> res = new ArrayList<>();
85+
List<String> ans = new ArrayList<>();
8386
for (int i = 0; i < 26; ++i) {
84-
while (freq[i]-- > 0) {
85-
res.add(String.valueOf((char) (i + 'a')));
87+
while (cnt[i]-- > 0) {
88+
ans.add(String.valueOf((char) (i + 'a')));
8689
}
8790
}
88-
return res;
91+
return ans;
8992
}
9093
}
9194
```
@@ -96,48 +99,52 @@ class Solution {
9699
class Solution {
97100
public:
98101
vector<string> commonChars(vector<string>& words) {
99-
vector<int> freq(26, 10000);
100-
for (auto word : words) {
101-
vector<int> t(26);
102-
for (char c : word)
103-
++t[c - 'a'];
104-
for (int i = 0; i < 26; ++i)
105-
freq[i] = min(freq[i], t[i]);
102+
int cnt[26];
103+
memset(cnt, 0x3f, sizeof(cnt));
104+
for (auto& w : words) {
105+
int ccnt[26]{};
106+
for (char& c : w) {
107+
++ccnt[c - 'a'];
108+
}
109+
for (int i = 0; i < 26; ++i) {
110+
cnt[i] = min(cnt[i], ccnt[i]);
111+
}
106112
}
107-
vector<string> res;
108-
for (int i = 0; i < 26; i++) {
109-
while (freq[i]--)
110-
res.emplace_back(1, i + 'a');
113+
vector<string> ans;
114+
for (int i = 0; i < 26; ++i) {
115+
while (cnt[i]--) {
116+
ans.emplace_back(1, i + 'a');
117+
}
111118
}
112-
return res;
119+
return ans;
113120
}
114121
};
115122
```
116123
117124
### **Go**
118125
119126
```go
120-
func commonChars(words []string) []string {
121-
freq := make([]int, 26)
122-
for i := 0; i < 26; i++ {
123-
freq[i] = 10000
127+
func commonChars(words []string) (ans []string) {
128+
cnt := [26]int{}
129+
for i := range cnt {
130+
cnt[i] = 1 << 30
124131
}
125-
for _, word := range words {
126-
t := make([]int, 26)
127-
for _, c := range word {
128-
t[c-'a']++
132+
for _, w := range words {
133+
ccnt := [26]int{}
134+
for _, c := range w {
135+
ccnt[c-'a']++
129136
}
130-
for i := 0; i < 26; i++ {
131-
freq[i] = min(freq[i], t[i])
137+
for i, v := range cnt {
138+
cnt[i] = min(v, ccnt[i])
132139
}
133140
}
134-
var res []string
135-
for i := 0; i < 26; i++ {
136-
for j := 0; j < freq[i]; j++ {
137-
res = append(res, string('a'+i))
141+
for i, v := range cnt {
142+
for v > 0 {
143+
ans = append(ans, string(i+'a'))
144+
v--
138145
}
139146
}
140-
return res
147+
return
141148
}
142149
143150
func min(a, b int) int {

solution/1000-1099/1002.Find Common Characters/README_EN.md

+51-50
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,40 @@
3232
```python
3333
class Solution:
3434
def commonChars(self, words: List[str]) -> List[str]:
35-
freq = [10000] * 26
36-
for word in words:
37-
t = [0] * 26
38-
for c in word:
39-
t[ord(c) - ord('a')] += 1
40-
for i in range(26):
41-
freq[i] = min(freq[i], t[i])
42-
res = []
43-
for i in range(26):
44-
if freq[i] > 0:
45-
res.extend([chr(i + ord("a"))] * freq[i])
46-
return res
35+
cnt = Counter(words[0])
36+
for w in words:
37+
ccnt = Counter(w)
38+
for c in cnt.keys():
39+
cnt[c] = min(cnt[c], ccnt[c])
40+
ans = []
41+
for c, v in cnt.items():
42+
ans.extend([c] * v)
43+
return ans
4744
```
4845

4946
### **Java**
5047

5148
```java
5249
class Solution {
5350
public List<String> commonChars(String[] words) {
54-
int[] freq = new int[26];
55-
Arrays.fill(freq, 10000);
56-
for (String word : words) {
57-
int[] t = new int[26];
58-
for (char c : word.toCharArray()) {
59-
++t[c - 'a'];
51+
int[] cnt = new int[26];
52+
Arrays.fill(cnt, 10000);
53+
for (String w : words) {
54+
int[] ccnt = new int[26];
55+
for (int i = 0; i < w.length(); ++i) {
56+
++ccnt[w.charAt(i) - 'a'];
6057
}
6158
for (int i = 0; i < 26; ++i) {
62-
freq[i] = Math.min(freq[i], t[i]);
59+
cnt[i] = Math.min(cnt[i], ccnt[i]);
6360
}
6461
}
65-
List<String> res = new ArrayList<>();
62+
List<String> ans = new ArrayList<>();
6663
for (int i = 0; i < 26; ++i) {
67-
while (freq[i]-- > 0) {
68-
res.add(String.valueOf((char) (i + 'a')));
64+
while (cnt[i]-- > 0) {
65+
ans.add(String.valueOf((char) (i + 'a')));
6966
}
7067
}
71-
return res;
68+
return ans;
7269
}
7370
}
7471
```
@@ -79,48 +76,52 @@ class Solution {
7976
class Solution {
8077
public:
8178
vector<string> commonChars(vector<string>& words) {
82-
vector<int> freq(26, 10000);
83-
for (auto word : words) {
84-
vector<int> t(26);
85-
for (char c : word)
86-
++t[c - 'a'];
87-
for (int i = 0; i < 26; ++i)
88-
freq[i] = min(freq[i], t[i]);
79+
int cnt[26];
80+
memset(cnt, 0x3f, sizeof(cnt));
81+
for (auto& w : words) {
82+
int ccnt[26]{};
83+
for (char& c : w) {
84+
++ccnt[c - 'a'];
85+
}
86+
for (int i = 0; i < 26; ++i) {
87+
cnt[i] = min(cnt[i], ccnt[i]);
88+
}
8989
}
90-
vector<string> res;
91-
for (int i = 0; i < 26; i++) {
92-
while (freq[i]--)
93-
res.emplace_back(1, i + 'a');
90+
vector<string> ans;
91+
for (int i = 0; i < 26; ++i) {
92+
while (cnt[i]--) {
93+
ans.emplace_back(1, i + 'a');
94+
}
9495
}
95-
return res;
96+
return ans;
9697
}
9798
};
9899
```
99100
100101
### **Go**
101102
102103
```go
103-
func commonChars(words []string) []string {
104-
freq := make([]int, 26)
105-
for i := 0; i < 26; i++ {
106-
freq[i] = 10000
104+
func commonChars(words []string) (ans []string) {
105+
cnt := [26]int{}
106+
for i := range cnt {
107+
cnt[i] = 1 << 30
107108
}
108-
for _, word := range words {
109-
t := make([]int, 26)
110-
for _, c := range word {
111-
t[c-'a']++
109+
for _, w := range words {
110+
ccnt := [26]int{}
111+
for _, c := range w {
112+
ccnt[c-'a']++
112113
}
113-
for i := 0; i < 26; i++ {
114-
freq[i] = min(freq[i], t[i])
114+
for i, v := range cnt {
115+
cnt[i] = min(v, ccnt[i])
115116
}
116117
}
117-
var res []string
118-
for i := 0; i < 26; i++ {
119-
for j := 0; j < freq[i]; j++ {
120-
res = append(res, string('a'+i))
118+
for i, v := range cnt {
119+
for v > 0 {
120+
ans = append(ans, string(i+'a'))
121+
v--
121122
}
122123
}
123-
return res
124+
return
124125
}
125126
126127
func min(a, b int) int {
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
class Solution {
22
public:
33
vector<string> commonChars(vector<string>& words) {
4-
vector<int> freq(26, 10000);
5-
for (auto word : words) {
6-
vector<int> t(26);
7-
for (char c : word)
8-
++t[c - 'a'];
9-
for (int i = 0; i < 26; ++i)
10-
freq[i] = min(freq[i], t[i]);
4+
int cnt[26];
5+
memset(cnt, 0x3f, sizeof(cnt));
6+
for (auto& w : words) {
7+
int ccnt[26]{};
8+
for (char& c : w) {
9+
++ccnt[c - 'a'];
10+
}
11+
for (int i = 0; i < 26; ++i) {
12+
cnt[i] = min(cnt[i], ccnt[i]);
13+
}
1114
}
12-
vector<string> res;
13-
for (int i = 0; i < 26; i++) {
14-
while (freq[i]--)
15-
res.emplace_back(1, i + 'a');
15+
vector<string> ans;
16+
for (int i = 0; i < 26; ++i) {
17+
while (cnt[i]--) {
18+
ans.emplace_back(1, i + 'a');
19+
}
1620
}
17-
return res;
21+
return ans;
1822
}
1923
};

solution/1000-1099/1002.Find Common Characters/Solution.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
func commonChars(words []string) []string {
2-
freq := make([]int, 26)
3-
for i := 0; i < 26; i++ {
4-
freq[i] = 10000
1+
func commonChars(words []string) (ans []string) {
2+
cnt := [26]int{}
3+
for i := range cnt {
4+
cnt[i] = 1 << 30
55
}
6-
for _, word := range words {
7-
t := make([]int, 26)
8-
for _, c := range word {
9-
t[c-'a']++
6+
for _, w := range words {
7+
ccnt := [26]int{}
8+
for _, c := range w {
9+
ccnt[c-'a']++
1010
}
11-
for i := 0; i < 26; i++ {
12-
freq[i] = min(freq[i], t[i])
11+
for i, v := range cnt {
12+
cnt[i] = min(v, ccnt[i])
1313
}
1414
}
15-
var res []string
16-
for i := 0; i < 26; i++ {
17-
for j := 0; j < freq[i]; j++ {
18-
res = append(res, string('a'+i))
15+
for i, v := range cnt {
16+
for v > 0 {
17+
ans = append(ans, string(i+'a'))
18+
v--
1919
}
2020
}
21-
return res
21+
return
2222
}
2323

2424
func min(a, b int) int {

0 commit comments

Comments
 (0)