LeetCode Entry
3120. Count the Number of Special Characters I
Count letters with both cases
3120. Count the Number of Special Characters I easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1371
Problem TLDR
Count letters with both cases
Intuition
Brute-force is accepted. O(n) memory: for any uniq lowercase check if uppercase present, use hashset O(1) memory: two bitmasks, for lower and for upper cases; (m & M) count bits is the result
Approach
- regex is ugly here
Complexity
-
Time complexity: \(O(n^2)\)
-
Space complexity: \(O(n)\)
Code
fun numberOfSpecialChars(w: String) =
w.toSet().count {it-32 in w}
pub fn number_of_special_chars(w: String) -> i32 {
('a'..='z').zip('A'..='Z').filter(|&(c,C)| w.contains(c)&&w.contains(C)).count() as _
}
Comments