diff --git a/src/problem/mod.rs b/src/problem/mod.rs index f4c7c94..33f2902 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -570,3 +570,5 @@ mod p2716_minimize_string_length; mod p2360_longest_cycle_in_a_graph; mod p2109_adding_spaces_to_a_string; + +mod p2278_percentage_of_letter_in_string; diff --git a/src/problem/p2278_percentage_of_letter_in_string.rs b/src/problem/p2278_percentage_of_letter_in_string.rs new file mode 100644 index 0000000..5b27b71 --- /dev/null +++ b/src/problem/p2278_percentage_of_letter_in_string.rs @@ -0,0 +1,25 @@ +/** + * [2278] Percentage of Letter in String + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn percentage_letter(s: String, letter: char) -> i32 { + (s.chars().filter(|c| *c == letter).count() * 100 / s.len()) as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2278() { + assert_eq!(33, Solution::percentage_letter("foobar".to_owned(), 'o')); + assert_eq!(0, Solution::percentage_letter("jjjj".to_owned(), 'k')); + } +}