Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
Created September 27, 2021 14:50
Show Gist options
  • Save AnthonyMikh/efe537463cf3499e8916d619926230ba to your computer and use it in GitHub Desktop.
Save AnthonyMikh/efe537463cf3499e8916d619926230ba to your computer and use it in GitHub Desktop.
Решение задачи №285 от UniLecs
fn split_consecutive_identical_str(s: &str) -> impl Iterator<Item = (u8, usize)> + '_ {
split_consecutive_identical(s.as_bytes())
}
fn split_consecutive_identical(mut seq: &[u8]) -> impl Iterator<Item = (u8, usize)> + '_ {
std::iter::from_fn(move || {
let (&first, rest) = seq.split_first()?;
let cut_off = rest
.iter()
.position(|&ch| ch != first)
.unwrap_or(rest.len());
seq = &rest[cut_off..];
Some((first, cut_off + 1))
})
}
fn is_long_pressed(original: &str, typed: &str) -> bool {
let mut original = split_consecutive_identical_str(original);
original
.by_ref()
.zip(split_consecutive_identical_str(typed))
.all(|((ch1, rep1), (ch2, rep2))| ch1 == ch2 && rep2 >= rep1)
&& original.next().is_none()
}
fn main() {
let test_cases = [
("albert", "albert", true),
("albert", "aaalberrt", true),
("lee", "lle", false),
];
for (original, typed, expected_outcome) in test_cases {
assert!(is_long_pressed(original, typed) == expected_outcome);
}
}
@AnthonyMikh
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment