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 b60dc87

Browse files
feat: add solutions to lc problem: No.3692 (doocs#4757)
1 parent 264b8f0 commit b60dc87

File tree

7 files changed

+373
-8
lines changed

7 files changed

+373
-8
lines changed

‎solution/3600-3699/3692.Majority Frequency Characters/README.md‎

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,156 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3692.Ma
156156

157157
<!-- solution:start -->
158158

159-
### 方法一
159+
### 方法一:哈希表
160+
161+
我们先用一个数组或哈希表 $\textit{cnt}$ 统计字符串中每个字符的出现频率。然后,我们再用一个哈希表 $\textit{f},ドル将出现频率 $k$ 相同的字符放在同一个列表中,即 $\textit{f}[k]$ 存储所有出现频率为 $k$ 的字符。
162+
163+
接下来,我们遍历哈希表 $\textit{f},ドル找到组大小最大的频率组。如果有多个频率组的大小并列最大,则选择其频率 $k$ 较大的那个组。最后,我们将该频率组中的所有字符连接成一个字符串并返回。
164+
165+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为字符串 $\textit{s}$ 的长度。
160166

161167
<!-- tabs:start -->
162168

163169
#### Python3
164170

165171
```python
166-
172+
class Solution:
173+
def majorityFrequencyGroup(self, s: str) -> str:
174+
cnt = Counter(s)
175+
f = defaultdict(list)
176+
for c, v in cnt.items():
177+
f[v].append(c)
178+
mx = mv = 0
179+
ans = []
180+
for v, cs in f.items():
181+
if mx < len(cs) or (mx == len(cs) and mv < v):
182+
mx = len(cs)
183+
mv = v
184+
ans = cs
185+
return "".join(ans)
167186
```
168187

169188
#### Java
170189

171190
```java
172-
191+
class Solution {
192+
public String majorityFrequencyGroup(String s) {
193+
int[] cnt = new int[26];
194+
for (char c : s.toCharArray()) {
195+
++cnt[c - 'a'];
196+
}
197+
Map<Integer, StringBuilder> f = new HashMap<>();
198+
for (int i = 0; i < cnt.length; ++i) {
199+
if (cnt[i] > 0) {
200+
f.computeIfAbsent(cnt[i], k -> new StringBuilder()).append((char) ('a' + i));
201+
}
202+
}
203+
int mx = 0;
204+
int mv = 0;
205+
String ans = "";
206+
for (var e : f.entrySet()) {
207+
int v = e.getKey();
208+
var cs = e.getValue();
209+
if (mx < cs.length() || (mx == cs.length() && mv < v)) {
210+
mx = cs.length();
211+
mv = v;
212+
ans = cs.toString();
213+
}
214+
}
215+
return ans;
216+
}
217+
}
173218
```
174219

175220
#### C++
176221

177222
```cpp
178-
223+
class Solution {
224+
public:
225+
string majorityFrequencyGroup(string s) {
226+
vector<int> cnt(26, 0);
227+
for (char c : s) {
228+
++cnt[c - 'a'];
229+
}
230+
231+
unordered_map<int, string> f;
232+
for (int i = 0; i < 26; ++i) {
233+
if (cnt[i] > 0) {
234+
f[cnt[i]].push_back('a' + i);
235+
}
236+
}
237+
238+
int mx = 0, mv = 0;
239+
string ans;
240+
for (auto& e : f) {
241+
int v = e.first;
242+
string& cs = e.second;
243+
if (mx < (int) cs.size() || (mx == (int) cs.size() && mv < v)) {
244+
mx = cs.size();
245+
mv = v;
246+
ans = cs;
247+
}
248+
}
249+
return ans;
250+
}
251+
};
179252
```
180253

181254
#### Go
182255

183256
```go
257+
func majorityFrequencyGroup(s string) string {
258+
cnt := make([]int, 26)
259+
for _, c := range s {
260+
cnt[c-'a']++
261+
}
262+
263+
f := make(map[int][]byte)
264+
for i, v := range cnt {
265+
if v > 0 {
266+
f[v] = append(f[v], byte('a'+i))
267+
}
268+
}
269+
270+
mx, mv := 0, 0
271+
var ans []byte
272+
for v, cs := range f {
273+
if len(cs) > mx || (len(cs) == mx && v > mv) {
274+
mx = len(cs)
275+
mv = v
276+
ans = cs
277+
}
278+
}
279+
return string(ans)
280+
}
281+
```
184282

283+
#### TypeScript
284+
285+
```ts
286+
function majorityFrequencyGroup(s: string): string {
287+
const cnt: Record<string, number> = {};
288+
for (const c of s) {
289+
cnt[c] = (cnt[c] || 0) + 1;
290+
}
291+
const f = new Map<number, string[]>();
292+
for (const [c, v] of Object.entries(cnt)) {
293+
if (!f.has(v)) {
294+
f.set(v, []);
295+
}
296+
f.get(v)!.push(c);
297+
}
298+
let [mx, mv] = [0, 0];
299+
let ans = '';
300+
f.forEach((cs, v) => {
301+
if (mx < cs.length || (mx == cs.length && mv < v)) {
302+
mx = cs.length;
303+
mv = v;
304+
ans = cs.join('');
305+
}
306+
});
307+
return ans;
308+
}
185309
```
186310

187311
<!-- tabs:end -->

‎solution/3600-3699/3692.Majority Frequency Characters/README_EN.md‎

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,32 +154,156 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3692.Ma
154154

155155
<!-- solution:start -->
156156

157-
### Solution 1
157+
### Solution: Hash Table
158+
159+
We first use an array or hash table $\textit{cnt}$ to count the frequency of each character in the string. Then, we use another hash table $\textit{f}$ to group characters with the same frequency $k$ into the same list, i.e., $\textit{f}[k]$ stores all characters with frequency $k$.
160+
161+
Next, we iterate through the hash table $\textit{f}$ to find the frequency group with the maximum group size. If multiple frequency groups have the same maximum size, we choose the one with the larger frequency $k$. Finally, we concatenate all characters in that frequency group into a string and return it.
162+
163+
The time complexity is $O(n)$ and the space complexity is $O(n),ドル where $n$ is the length of string $\textit{s}$.
158164

159165
<!-- tabs:start -->
160166

161167
#### Python3
162168

163169
```python
164-
170+
class Solution:
171+
def majorityFrequencyGroup(self, s: str) -> str:
172+
cnt = Counter(s)
173+
f = defaultdict(list)
174+
for c, v in cnt.items():
175+
f[v].append(c)
176+
mx = mv = 0
177+
ans = []
178+
for v, cs in f.items():
179+
if mx < len(cs) or (mx == len(cs) and mv < v):
180+
mx = len(cs)
181+
mv = v
182+
ans = cs
183+
return "".join(ans)
165184
```
166185

167186
#### Java
168187

169188
```java
170-
189+
class Solution {
190+
public String majorityFrequencyGroup(String s) {
191+
int[] cnt = new int[26];
192+
for (char c : s.toCharArray()) {
193+
++cnt[c - 'a'];
194+
}
195+
Map<Integer, StringBuilder> f = new HashMap<>();
196+
for (int i = 0; i < cnt.length; ++i) {
197+
if (cnt[i] > 0) {
198+
f.computeIfAbsent(cnt[i], k -> new StringBuilder()).append((char) ('a' + i));
199+
}
200+
}
201+
int mx = 0;
202+
int mv = 0;
203+
String ans = "";
204+
for (var e : f.entrySet()) {
205+
int v = e.getKey();
206+
var cs = e.getValue();
207+
if (mx < cs.length() || (mx == cs.length() && mv < v)) {
208+
mx = cs.length();
209+
mv = v;
210+
ans = cs.toString();
211+
}
212+
}
213+
return ans;
214+
}
215+
}
171216
```
172217

173218
#### C++
174219

175220
```cpp
176-
221+
class Solution {
222+
public:
223+
string majorityFrequencyGroup(string s) {
224+
vector<int> cnt(26, 0);
225+
for (char c : s) {
226+
++cnt[c - 'a'];
227+
}
228+
229+
unordered_map<int, string> f;
230+
for (int i = 0; i < 26; ++i) {
231+
if (cnt[i] > 0) {
232+
f[cnt[i]].push_back('a' + i);
233+
}
234+
}
235+
236+
int mx = 0, mv = 0;
237+
string ans;
238+
for (auto& e : f) {
239+
int v = e.first;
240+
string& cs = e.second;
241+
if (mx < (int) cs.size() || (mx == (int) cs.size() && mv < v)) {
242+
mx = cs.size();
243+
mv = v;
244+
ans = cs;
245+
}
246+
}
247+
return ans;
248+
}
249+
};
177250
```
178251

179252
#### Go
180253

181254
```go
255+
func majorityFrequencyGroup(s string) string {
256+
cnt := make([]int, 26)
257+
for _, c := range s {
258+
cnt[c-'a']++
259+
}
260+
261+
f := make(map[int][]byte)
262+
for i, v := range cnt {
263+
if v > 0 {
264+
f[v] = append(f[v], byte('a'+i))
265+
}
266+
}
267+
268+
mx, mv := 0, 0
269+
var ans []byte
270+
for v, cs := range f {
271+
if len(cs) > mx || (len(cs) == mx && v > mv) {
272+
mx = len(cs)
273+
mv = v
274+
ans = cs
275+
}
276+
}
277+
return string(ans)
278+
}
279+
```
182280

281+
#### TypeScript
282+
283+
```ts
284+
function majorityFrequencyGroup(s: string): string {
285+
const cnt: Record<string, number> = {};
286+
for (const c of s) {
287+
cnt[c] = (cnt[c] || 0) + 1;
288+
}
289+
const f = new Map<number, string[]>();
290+
for (const [c, v] of Object.entries(cnt)) {
291+
if (!f.has(v)) {
292+
f.set(v, []);
293+
}
294+
f.get(v)!.push(c);
295+
}
296+
let [mx, mv] = [0, 0];
297+
let ans = '';
298+
f.forEach((cs, v) => {
299+
if (mx < cs.length || (mx == cs.length && mv < v)) {
300+
mx = cs.length;
301+
mv = v;
302+
ans = cs.join('');
303+
}
304+
});
305+
return ans;
306+
}
183307
```
184308

185309
<!-- tabs:end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
string majorityFrequencyGroup(string s) {
4+
vector<int> cnt(26, 0);
5+
for (char c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
9+
unordered_map<int, string> f;
10+
for (int i = 0; i < 26; ++i) {
11+
if (cnt[i] > 0) {
12+
f[cnt[i]].push_back('a' + i);
13+
}
14+
}
15+
16+
int mx = 0, mv = 0;
17+
string ans;
18+
for (auto& e : f) {
19+
int v = e.first;
20+
string& cs = e.second;
21+
if (mx < (int) cs.size() || (mx == (int) cs.size() && mv < v)) {
22+
mx = cs.size();
23+
mv = v;
24+
ans = cs;
25+
}
26+
}
27+
return ans;
28+
}
29+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func majorityFrequencyGroup(s string) string {
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
7+
f := make(map[int][]byte)
8+
for i, v := range cnt {
9+
if v > 0 {
10+
f[v] = append(f[v], byte('a'+i))
11+
}
12+
}
13+
14+
mx, mv := 0, 0
15+
var ans []byte
16+
for v, cs := range f {
17+
if len(cs) > mx || (len(cs) == mx && v > mv) {
18+
mx = len(cs)
19+
mv = v
20+
ans = cs
21+
}
22+
}
23+
return string(ans)
24+
}

0 commit comments

Comments
(0)

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