commit dc009d327e278bea38e4c981404a897accf278b8
parent fa0ba4e434a78e3da70ad6665cdeddc71603b8ee
Author: Jack Sangdahl <jack@pngu.org>
Date: Tue, 26 May 2026 05:47:47 +0300
3120. count the number of special characters 1
Diffstat:
2 files changed, 54 insertions(+), 0 deletions(-)
diff --git a/count_num_special_char1.c b/count_num_special_char1.c
@@ -0,0 +1,35 @@
+// 3120. Count the Number of Special Characters I
+#include "leetcode.h"
+
+/*
+ * 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 characters
+ * in 'word'.
+ */
+
+int numberOfSpecialChars(char *word) {
+ int lower = 0, upper = 0, n = strlen(word);
+ for (int i = 0; i < n; i++) {
+ if (word[i] >= 'a' && word[i] <= 'z')
+ lower |= (1 << (word[i] - 'a'));
+ else
+ upper |= (1 << (word[i] - 'a'));
+ }
+ int common = lower & upper;
+ return __builtin_popcount(common);
+}
+
+int main() {
+ char *w1 = "aaAbcBC";
+ char *w2 = "abc";
+ char *w3 = "abBCab";
+ int r1 = numberOfSpecialChars(w1);
+ int r2 = numberOfSpecialChars(w2);
+ int r3 = numberOfSpecialChars(w3);
+ printf("%d\n", r1);
+ assert(r1 == 3);
+ printf("%d\n", r2);
+ assert(r2 == 0);
+ printf("%d\n", r3);
+ assert(r3 == 1);
+}
diff --git a/count_num_special_char1.py b/count_num_special_char1.py
@@ -0,0 +1,19 @@
+# 3120. 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 characters
+in 'word'.
+"""
+
+
+class Solution:
+ def numberOfSpecialChars(self, word: str) -> int:
+ return (s := {*word}) and sum(chr(ord(c) + 32) in s for c in s)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.numberOfSpecialChars(word="aaAbcBC"))
+ print(obj.numberOfSpecialChars(word="abc"))
+ print(obj.numberOfSpecialChars(word="abBCab"))