Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d1a3103

Browse files
committed
feat: add solutions to lc problem: No.1002.Find Common Characters
1 parent 77a9b98 commit d1a3103

File tree

7 files changed

+267
-80
lines changed

7 files changed

+267
-80
lines changed

‎solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class Solution {
163163
}
164164
```
165165

166+
### **C++**
167+
166168
```cpp
167169
class Solution {
168170
public:

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

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,60 +46,109 @@
4646
<!-- 这里可写当前语言的特殊实现逻辑 -->
4747

4848
```python
49-
49+
class Solution:
50+
def commonChars(self, words: List[str]) -> List[str]:
51+
freq = [10000] * 26
52+
for word in words:
53+
t = [0] * 26
54+
for c in word:
55+
t[ord(c) - ord('a')] += 1
56+
for i in range(26):
57+
freq[i] = min(freq[i], t[i])
58+
res = []
59+
for i in range(26):
60+
if freq[i] > 0:
61+
res.extend([chr(i + ord("a"))] * freq[i])
62+
return res
5063
```
5164

5265
### **Java**
5366

5467
<!-- 这里可写当前语言的特殊实现逻辑 -->
5568

5669
```java
57-
70+
class Solution {
71+
public List<String> commonChars(String[] words) {
72+
int[] freq = new int[26];
73+
Arrays.fill(freq, 10000);
74+
for (String word : words) {
75+
int[] t = new int[26];
76+
for (char c : word.toCharArray()) {
77+
++t[c - 'a'];
78+
}
79+
for (int i = 0; i < 26; ++i) {
80+
freq[i] = Math.min(freq[i], t[i]);
81+
}
82+
}
83+
List<String> res = new ArrayList<>();
84+
for (int i = 0; i < 26; ++i) {
85+
while (freq[i]-- > 0) {
86+
res.add(String.valueOf((char) (i + 'a')));
87+
}
88+
}
89+
return res;
90+
}
91+
}
5892
```
5993

60-
### **Go**
94+
### **C++**
6195

6296
```go
63-
func commonChars(A []string) []string {
64-
if len(A) == 0 {
65-
return []string{}
66-
}
67-
res := make([]int, 26)
68-
//以第一个字符串为基准,先统计出现次数
69-
for _, c := range A[0] {
70-
res[c - 'a']++
71-
}
72-
for i := 1; i < len(A); i++ {
73-
tmp := make([]int, 26)
74-
//统计后续每个字符串的字符出现次数
75-
for _, c := range A[i] {
76-
tmp[c - 'a']++
97+
class Solution {
98+
public:
99+
vector<string> commonChars(vector<string>& words) {
100+
vector<int> freq(26, 10000);
101+
for (auto word : words)
102+
{
103+
vector<int> t(26);
104+
for (char c : word)
105+
++t[c - 'a'];
106+
for (int i = 0; i < 26; ++i)
107+
freq[i] = min(freq[i], t[i]);
77108
}
78-
//比较,取小
79-
for j := 0; j < 26; j++ {
80-
res[j] = getMin(res[j], tmp[j])
109+
vector<string> res;
110+
for (int i = 0; i < 26; i++)
111+
{
112+
while (freq[i]--)
113+
res.emplace_back(1, i + 'a');
81114
}
115+
return res;
82116
}
83-
//遍历res,取出字符转换为string数组元素
84-
result := make([]string,0)
85-
for i := 0; i < len(res); i++ {
86-
if res[i] > 0 {
87-
for j := 0; j < res[i]; j++ {
88-
result = append(result, string('a' + i))
89-
}
90-
}
91-
}
92-
return result
93-
}
117+
};
118+
```
94119

95-
func getMin(a,b int) int {
96-
if a > b{
97-
return b
98-
}
99-
return a
120+
### **Go**
121+
122+
```go
123+
func commonChars(words []string) []string {
124+
freq := make([]int, 26)
125+
for i := 0; i < 26; i++ {
126+
freq[i] = 10000
127+
}
128+
for _, word := range words {
129+
t := make([]int, 26)
130+
for _, c := range word {
131+
t[c-'a']++
132+
}
133+
for i := 0; i < 26; i++ {
134+
freq[i] = min(freq[i], t[i])
135+
}
136+
}
137+
var res []string
138+
for i := 0; i < 26; i++ {
139+
for j := 0; j < freq[i]; j++ {
140+
res = append(res, string('a'+i))
141+
}
142+
}
143+
return res
100144
}
101145

146+
func min(a, b int) int {
147+
if a < b {
148+
return a
149+
}
150+
return b
151+
}
102152
```
103153

104154
<!-- tabs:end -->
105-
<!-- tabs:end -->

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

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,107 @@
7373
### **Python3**
7474

7575
```python
76-
76+
class Solution:
77+
def commonChars(self, words: List[str]) -> List[str]:
78+
freq = [10000] * 26
79+
for word in words:
80+
t = [0] * 26
81+
for c in word:
82+
t[ord(c) - ord('a')] += 1
83+
for i in range(26):
84+
freq[i] = min(freq[i], t[i])
85+
res = []
86+
for i in range(26):
87+
if freq[i] > 0:
88+
res.extend([chr(i + ord("a"))] * freq[i])
89+
return res
7790
```
7891

7992
### **Java**
8093

8194
```java
95+
class Solution {
96+
public List<String> commonChars(String[] words) {
97+
int[] freq = new int[26];
98+
Arrays.fill(freq, 10000);
99+
for (String word : words) {
100+
int[] t = new int[26];
101+
for (char c : word.toCharArray()) {
102+
++t[c - 'a'];
103+
}
104+
for (int i = 0; i < 26; ++i) {
105+
freq[i] = Math.min(freq[i], t[i]);
106+
}
107+
}
108+
List<String> res = new ArrayList<>();
109+
for (int i = 0; i < 26; ++i) {
110+
while (freq[i]-- > 0) {
111+
res.add(String.valueOf((char) (i + 'a')));
112+
}
113+
}
114+
return res;
115+
}
116+
}
117+
```
82118

119+
### **C++**
120+
121+
```go
122+
class Solution {
123+
public:
124+
vector<string> commonChars(vector<string>& words) {
125+
vector<int> freq(26, 10000);
126+
for (auto word : words)
127+
{
128+
vector<int> t(26);
129+
for (char c : word)
130+
++t[c - 'a'];
131+
for (int i = 0; i < 26; ++i)
132+
freq[i] = min(freq[i], t[i]);
133+
}
134+
vector<string> res;
135+
for (int i = 0; i < 26; i++)
136+
{
137+
while (freq[i]--)
138+
res.emplace_back(1, i + 'a');
139+
}
140+
return res;
141+
}
142+
};
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
func commonChars(words []string) []string {
149+
freq := make([]int, 26)
150+
for i := 0; i < 26; i++ {
151+
freq[i] = 10000
152+
}
153+
for _, word := range words {
154+
t := make([]int, 26)
155+
for _, c := range word {
156+
t[c-'a']++
157+
}
158+
for i := 0; i < 26; i++ {
159+
freq[i] = min(freq[i], t[i])
160+
}
161+
}
162+
var res []string
163+
for i := 0; i < 26; i++ {
164+
for j := 0; j < freq[i]; j++ {
165+
res = append(res, string('a'+i))
166+
}
167+
}
168+
return res
169+
}
170+
171+
func min(a, b int) int {
172+
if a < b {
173+
return a
174+
}
175+
return b
176+
}
83177
```
84178

85179
### **...**
@@ -89,4 +183,3 @@
89183
```
90184

91185
<!-- tabs:end -->
92-
<!-- tabs:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<string> commonChars(vector<string>& words) {
4+
vector<int> freq(26, 10000);
5+
for (auto word : words)
6+
{
7+
vector<int> t(26);
8+
for (char c : word)
9+
++t[c - 'a'];
10+
for (int i = 0; i < 26; ++i)
11+
freq[i] = min(freq[i], t[i]);
12+
}
13+
vector<string> res;
14+
for (int i = 0; i < 26; i++)
15+
{
16+
while (freq[i]--)
17+
res.emplace_back(1, i + 'a');
18+
}
19+
return res;
20+
}
21+
};
Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
1-
func commonChars(A []string) []string {
2-
if len(A) == 0 {
3-
return []string{}
4-
}
5-
res := make([]int, 26)
6-
//以第一个字符串为基准,先统计出现次数
7-
for _, c := range A[0] {
8-
res[c - 'a']++
9-
}
10-
for i := 1; i < len(A); i++ {
11-
tmp := make([]int, 26)
12-
//统计后续每个字符串的字符出现次数
13-
for _, c := range A[i] {
14-
tmp[c - 'a']++
15-
}
16-
//比较,取小
17-
for j := 0; j < 26; j++ {
18-
res[j] = getMin(res[j], tmp[j])
19-
}
20-
}
21-
//遍历res,取出字符转换为string数组元素
22-
result := make([]string,0)
23-
for i := 0; i < len(res); i++ {
24-
if res[i] > 0 {
25-
for j := 0; j < res[i]; j++ {
26-
result = append(result, string('a' + i))
27-
}
28-
}
29-
}
30-
return result
1+
func commonChars(words []string) []string {
2+
freq := make([]int, 26)
3+
for i := 0; i < 26; i++ {
4+
freq[i] = 10000
5+
}
6+
for _, word := range words {
7+
t := make([]int, 26)
8+
for _, c := range word {
9+
t[c-'a']++
10+
}
11+
for i := 0; i < 26; i++ {
12+
freq[i] = min(freq[i], t[i])
13+
}
14+
}
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))
19+
}
20+
}
21+
return res
3122
}
3223

33-
func getMin(a,b int) int {
34-
if a > b{
35-
return b
36-
}
37-
return a
38-
}
39-
40-
41-
42-
43-
24+
func min(a, b int) int {
25+
if a < b {
26+
return a
27+
}
28+
return b
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public List<String> commonChars(String[] words) {
3+
int[] freq = new int[26];
4+
Arrays.fill(freq, 10000);
5+
for (String word : words) {
6+
int[] t = new int[26];
7+
for (char c : word.toCharArray()) {
8+
++t[c - 'a'];
9+
}
10+
for (int i = 0; i < 26; ++i) {
11+
freq[i] = Math.min(freq[i], t[i]);
12+
}
13+
}
14+
List<String> res = new ArrayList<>();
15+
for (int i = 0; i < 26; ++i) {
16+
while (freq[i]-- > 0) {
17+
res.add(String.valueOf((char) (i + 'a')));
18+
}
19+
}
20+
return res;
21+
}
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /