MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / longest_nice_substring

Function longest_nice_substring

1763-ongest-nice-substring.rs:1–27  ·  view source on GitHub ↗
(s: String)

Source from the content-addressed store, hash-verified

1pub fn longest_nice_substring(s: String) -> String {
2 use std::collections::HashMap;
3 pub fn is_nice_str(s: &str) -> bool {
4 let mut mmp = HashMap::new();
5 for c in s.chars() {
6 let count = mmp.entry(c).or_insert(0);
7 *count += 1;
8 }
9 let keys: Vec<char> = mmp.into_keys().collect();
10 if keys.len() % 2 !=0 {
11 false
12 } else {
13 let mut tmp: Vec<_> = keys.iter().map(|e|e.to_lowercase().to_string()).collect();
14 tmp.sort_unstable();
15 tmp.dedup();
16 keys.len() % tmp.len() == 0 && keys.len() / tmp.len() == 2
17 }
18 }
19 for i in (2..=s.len()).rev() {
20 for j in 0..=(s.len()-i) {
21 if is_nice_str(&s[j..=j+i-1]) {
22 return s[j..=j+i-1].to_string();
23 }
24 }
25 }
26 String::new()
27}
28
29fn main() {
30 let s = "dDzeE".to_string();

Callers

nothing calls this directly

Calls 1

is_nice_strFunction · 0.85

Tested by

no test coverage detected