Created
November 28, 2023 23:20
-
-
Save JustSimplyKyle/65238552a03d92e7f2dd98a282620bdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "substring-mutual-letters" | |
version = "0.1.0" | |
edition = "2021" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
fn main() { | |
let repeatness = 1; | |
let input = "aBBdaaa"; | |
let all_substrings = all_substrings(input); | |
let processed = all_substrings | |
.iter() | |
.filter(|x| good_string(repeatness, x)) | |
.max_by_key(|x| x.len()); | |
if let Some(x) = processed { | |
println!("{}, {:?}", &x.len(), x); | |
} else { | |
println!("0"); | |
} | |
} | |
fn all_substrings(s: &str) -> HashSet<&str> { | |
let mut set = HashSet::new(); | |
for i in 0..s.len() { | |
for j in i..s.len() { | |
set.insert(&s[i..j]); | |
} | |
} | |
set | |
} | |
fn good_string(repeatness: usize, input: &str) -> bool { | |
let vec_char = Vec::from_iter(input.chars()); | |
let chunks = vec_char.chunks_exact(repeatness); | |
if !chunks.remainder().is_empty() { | |
return false; | |
} | |
let window_view = chunks | |
.map(|x| { | |
let all_upper = x.iter().all(|char| char.is_uppercase()); | |
let all_lower = x.iter().all(|char| char.is_lowercase()); | |
// first key is to keep track of whether each "chunk" is in a different state from each other | |
// second key is to ensure that with each "chunk", every character is either lowercase or all uppercase | |
(all_upper, all_upper || all_lower) | |
}) | |
.collect::<Vec<_>>(); | |
if vec_char.len() == repeatness && !window_view[0].1 { | |
return false; | |
} | |
window_view.windows(2).all(|x| x[0].1 && x[0].0 != x[1].0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment