Skip to content

Commit 1dd6ce1

Browse files
committed
majority_frequency_characters.rs
1 parent 807f27d commit 1dd6ce1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn majority_frequency_group(s: String) -> String {
5+
let mut mp = HashMap::new();
6+
for c in s.chars() {
7+
*mp.entry(c).or_insert(0) += 1;
8+
}
9+
10+
let mut ct = HashMap::new();
11+
let mut freq = 0;
12+
let mut ans = String::new();
13+
for (c, f) in mp {
14+
ct.entry(f).or_insert(String::new()).push(c);
15+
let v = ct.get(&f).unwrap();
16+
if v.len() > ans.len() || (v.len() == ans.len() && f > freq) {
17+
freq = f;
18+
ans = v.to_string();
19+
}
20+
}
21+
22+
ans
23+
}
24+
}

0 commit comments

Comments
 (0)