Skip to content

Commit b9ed3ae

Browse files
committed
Add problem 3120: Count the Number of Special Characters I
1 parent 9320778 commit b9ed3ae

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,7 @@ pub mod problem_3110_score_of_a_string;
20952095
pub mod problem_3111_minimum_rectangles_to_cover_points;
20962096
pub mod problem_3114_latest_time_you_can_obtain_after_replacing_characters;
20972097
pub mod problem_3115_maximum_prime_difference;
2098+
pub mod problem_3120_count_the_number_of_special_characters_i;
20982099
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
20992100

21002101
#[cfg(test)]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn number_of_special_chars(word: String) -> i32 {
7+
let mut states = [0_u32; 2];
8+
9+
for c in word.into_bytes() {
10+
states[usize::from(c >> 5) & 1] |= 1 << (c & 31);
11+
}
12+
13+
let [upper_cases, lower_cases] = states;
14+
15+
(upper_cases & lower_cases).count_ones().cast_signed()
16+
}
17+
}
18+
19+
// ------------------------------------------------------ snip ------------------------------------------------------ //
20+
21+
impl super::Solution for Solution {
22+
fn number_of_special_chars(word: String) -> i32 {
23+
Self::number_of_special_chars(word)
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
#[test]
30+
fn test_solution() {
31+
super::super::tests::run::<super::Solution>();
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod bit_manipulation;
2+
3+
pub trait Solution {
4+
fn number_of_special_chars(word: 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 = [("aaAbcBC", 3), ("abc", 0), ("abBCab", 1)];
13+
14+
for (word, expected) in test_cases {
15+
assert_eq!(S::number_of_special_chars(word.to_string()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)