Skip to content

Commit e9d5955

Browse files
committed
Add problem 3136: Valid Word
1 parent 53c0de4 commit e9d5955

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,7 @@ pub mod problem_3121_count_the_number_of_special_characters_ii;
21002100
pub mod problem_3127_make_a_square_with_the_same_color;
21012101
pub mod problem_3131_find_the_integer_added_to_array_i;
21022102
pub mod problem_3133_minimum_array_end;
2103+
pub mod problem_3136_valid_word;
21032104
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21042105

21052106
#[cfg(test)]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn is_valid(word: String) -> bool {
7+
word.len() > 2 && {
8+
word.bytes().try_fold(0_u8, |state, c| {
9+
let bit = match c {
10+
b'0'..=b'9' => 0,
11+
b'A' | b'E' | b'I' | b'O' | b'U' | b'a' | b'e' | b'i' | b'o' | b'u' => 1,
12+
b'B' | b'C' | b'D' | b'F' | b'G' | b'H' | b'J' | b'K' | b'L' | b'M' | b'N' | b'P' | b'Q' | b'R'
13+
| b'S' | b'T' | b'V' | b'W' | b'X' | b'Y' | b'Z' | b'b' | b'c' | b'd' | b'f' | b'g' | b'h'
14+
| b'j' | b'k' | b'l' | b'm' | b'n' | b'p' | b'q' | b'r' | b's' | b't' | b'v' | b'w' | b'x'
15+
| b'y' | b'z' => 2,
16+
_ => return None,
17+
};
18+
19+
Some(state | bit)
20+
}) == Some(3)
21+
}
22+
}
23+
}
24+
25+
// ------------------------------------------------------ snip ------------------------------------------------------ //
26+
27+
impl super::Solution for Solution {
28+
fn is_valid(word: String) -> bool {
29+
Self::is_valid(word)
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
#[test]
36+
fn test_solution() {
37+
super::super::tests::run::<super::Solution>();
38+
}
39+
}

src/problem_3136_valid_word/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn is_valid(word: String) -> bool;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [("234Adas", true), ("b3", false), ("a3$e", false), ("aya", true)];
13+
14+
for (word, expected) in test_cases {
15+
assert_eq!(S::is_valid(word.to_string()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)