3408 Count The Number Of Special Characters I
3408 Count The Number Of Special Characters I
Count the Number of Special Characters I 
You are given a string word. A letter is called special if it appears both in lowercase and uppercase in word.
Return the number of* special letters in *word.
Example 1:
Input: word = “aaAbcBC”
Output: 3
Explanation:
The special characters in word are ‘a’, ‘b’, and ‘c’.
Example 2:
Input: word = “abc”
Output: 0
Explanation:
No character in word appears in uppercase.
Example 3:
Input: word = “abBCab”
Output: 1
Explanation:
The only special character in word is ‘b’.
Constraints:
1
2
1 <= word.length <= 50
word consists of only lowercase and uppercase English letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pub fn number_of_special_chars(word: String) -> i32 {
use std::collections::HashSet;
let s: HashSet<char> = word.chars().collect();
let mut ans = 0;
for c in 'a'..='z' {
if s.contains(&c) && s.contains(&c.to_uppercase().next().unwrap()) {
ans += 1;
}
}
ans
}
}
This post is licensed under CC BY 4.0 by the author.