Skip to content

Commit ce39c86

Browse files
committed
Add problem 1935: Maximum Number of Words You Can Type
1 parent 02bb15f commit ce39c86

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ pub mod problem_1880_check_if_word_equals_summation_of_two_words;
635635
pub mod problem_1903_largest_odd_number_in_string;
636636
pub mod problem_1920_build_array_from_permutation;
637637
pub mod problem_1929_concatenation_of_array;
638+
pub mod problem_1935_maximum_number_of_words_you_can_type;
638639
pub mod problem_1961_check_if_string_is_a_prefix_of_array;
639640
pub mod problem_1962_remove_stones_to_minimize_the_total;
640641
pub mod problem_2032_two_out_of_three;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pub struct Solution;
2+
3+
impl Solution {
4+
pub fn can_be_typed_words(text: String, broken_letters: String) -> i32 {
5+
let mut broken = [false; 26];
6+
for byte in broken_letters.bytes() {
7+
broken[(byte - b'a') as usize] = true;
8+
}
9+
text.split(' ')
10+
.filter(|s| !s.bytes().any(|x| broken[(x - b'a') as usize]))
11+
.count() as _
12+
}
13+
}
14+
15+
impl super::Solution for Solution {
16+
fn can_be_typed_words(text: String, broken_letters: String) -> i32 {
17+
Self::can_be_typed_words(text, broken_letters)
18+
}
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
#[test]
24+
fn test_solution() {
25+
super::super::tests::run::<super::Solution>();
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn can_be_typed_words(text: String, broken_letters: String) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(("hello world", "ad"), 1),
14+
(("leet code", "lt"), 1),
15+
(("leet code", "e"), 0),
16+
];
17+
18+
for ((text, broken_letters), expected) in test_cases {
19+
assert_eq!(
20+
S::can_be_typed_words(text.to_string(), broken_letters.to_string()),
21+
expected,
22+
);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)