From 63e95c695d95317810536b3367c6626b27862be4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 16 Jun 2025 14:46:42 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3581 No.3581.Count Odd Letters from Number --- .../README.md | 148 +++++++++++++++++- .../README_EN.md | 148 +++++++++++++++++- .../Solution.cpp | 26 +++ .../Solution.go | 25 +++ .../Solution.java | 27 ++++ .../Solution.py | 23 +++ .../Solution.ts | 34 ++++ 7 files changed, 423 insertions(+), 8 deletions(-) create mode 100644 solution/3500-3599/3581.Count Odd Letters from Number/Solution.cpp create mode 100644 solution/3500-3599/3581.Count Odd Letters from Number/Solution.go create mode 100644 solution/3500-3599/3581.Count Odd Letters from Number/Solution.java create mode 100644 solution/3500-3599/3581.Count Odd Letters from Number/Solution.py create mode 100644 solution/3500-3599/3581.Count Odd Letters from Number/Solution.ts diff --git a/solution/3500-3599/3581.Count Odd Letters from Number/README.md b/solution/3500-3599/3581.Count Odd Letters from Number/README.md index ce195c8c2c2ee..0fd13c5cf7c8d 100644 --- a/solution/3500-3599/3581.Count Odd Letters from Number/README.md +++ b/solution/3500-3599/3581.Count Odd Letters from Number/README.md @@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3581.Co -### 方法一 +### 方法一:模拟 + 位运算 + +我们可以将每个数字转换为对应的英文单词,然后统计每个字母出现的次数。由于字母的数量有限,我们可以使用一个整数 $\textit{mask}$ 来表示每个字母的出现情况。具体地,我们可以将字母映射到整数的二进制位上,如果某个字母出现了奇数次,则对应的二进制位为 1,否则为 0。最后,我们只需要统计 $\textit{mask}$ 中为 1 的位数,即为答案。 + +时间复杂度 $O(\log n)$,其中 $n$ 是输入的整数。空间复杂度 $O(1)$。 #### Python3 ```python - +d = { + 0: "zero", + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine", +} + + +class Solution: + def countOddLetters(self, n: int) -> int: + mask = 0 + while n: + x = n % 10 + n //= 10 + for c in d[x]: + mask ^= 1 << (ord(c) - ord("a")) + return mask.bit_count() ``` #### Java ```java - +class Solution { + private static final Map d = new HashMap<>(); + static { + d.put(0, "zero"); + d.put(1, "one"); + d.put(2, "two"); + d.put(3, "three"); + d.put(4, "four"); + d.put(5, "five"); + d.put(6, "six"); + d.put(7, "seven"); + d.put(8, "eight"); + d.put(9, "nine"); + } + + public int countOddLetters(int n) { + int mask = 0; + while (n > 0) { + int x = n % 10; + n /= 10; + for (char c : d.get(x).toCharArray()) { + mask ^= 1 << (c - 'a'); + } + } + return Integer.bitCount(mask); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countOddLetters(int n) { + static const unordered_map d = { + {0, "zero"}, + {1, "one"}, + {2, "two"}, + {3, "three"}, + {4, "four"}, + {5, "five"}, + {6, "six"}, + {7, "seven"}, + {8, "eight"}, + {9, "nine"}}; + + int mask = 0; + while (n > 0) { + int x = n % 10; + n /= 10; + for (char c : d.at(x)) { + mask ^= 1 << (c - 'a'); + } + } + return __builtin_popcount(mask); + } +}; ``` #### Go ```go +func countOddLetters(n int) int { + d := map[int]string{ + 0: "zero", + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine", + } + + mask := 0 + for n > 0 { + x := n % 10 + n /= 10 + for _, c := range d[x] { + mask ^= 1 << (c - 'a') + } + } + + return bits.OnesCount32(uint32(mask)) +} +``` +#### TypeScript + +```ts +function countOddLetters(n: number): number { + const d: Record = { + 0: 'zero', + 1: 'one', + 2: 'two', + 3: 'three', + 4: 'four', + 5: 'five', + 6: 'six', + 7: 'seven', + 8: 'eight', + 9: 'nine', + }; + + let mask = 0; + while (n > 0) { + const x = n % 10; + n = Math.floor(n / 10); + for (const c of d[x]) { + mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0)); + } + } + + return bitCount(mask); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} ``` diff --git a/solution/3500-3599/3581.Count Odd Letters from Number/README_EN.md b/solution/3500-3599/3581.Count Odd Letters from Number/README_EN.md index 3fd3d88a6349d..693f1c94b6e8f 100644 --- a/solution/3500-3599/3581.Count Odd Letters from Number/README_EN.md +++ b/solution/3500-3599/3581.Count Odd Letters from Number/README_EN.md @@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3581.Co -### Solution 1 +### Solution 1: Simulation + Bit Manipulation + +We can convert each number into its corresponding English word, then count the frequency of each letter. Since the number of letters is limited, we can use an integer $\textit{mask}$ to represent the occurrence of each letter. Specifically, we can map each letter to a binary bit of the integer. If a letter appears an odd number of times, the corresponding binary bit is 1; otherwise, it's 0. Finally, we only need to count the number of bits that are 1 in $\textit{mask}$, which is the answer. + +The time complexity is $O(\log n)$, where $n$ is the input integer. And the space complexity is $O(1)$. #### Python3 ```python - +d = { + 0: "zero", + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine", +} + + +class Solution: + def countOddLetters(self, n: int) -> int: + mask = 0 + while n: + x = n % 10 + n //= 10 + for c in d[x]: + mask ^= 1 << (ord(c) - ord("a")) + return mask.bit_count() ``` #### Java ```java - +class Solution { + private static final Map d = new HashMap<>(); + static { + d.put(0, "zero"); + d.put(1, "one"); + d.put(2, "two"); + d.put(3, "three"); + d.put(4, "four"); + d.put(5, "five"); + d.put(6, "six"); + d.put(7, "seven"); + d.put(8, "eight"); + d.put(9, "nine"); + } + + public int countOddLetters(int n) { + int mask = 0; + while (n > 0) { + int x = n % 10; + n /= 10; + for (char c : d.get(x).toCharArray()) { + mask ^= 1 << (c - 'a'); + } + } + return Integer.bitCount(mask); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countOddLetters(int n) { + static const unordered_map d = { + {0, "zero"}, + {1, "one"}, + {2, "two"}, + {3, "three"}, + {4, "four"}, + {5, "five"}, + {6, "six"}, + {7, "seven"}, + {8, "eight"}, + {9, "nine"}}; + + int mask = 0; + while (n > 0) { + int x = n % 10; + n /= 10; + for (char c : d.at(x)) { + mask ^= 1 << (c - 'a'); + } + } + return __builtin_popcount(mask); + } +}; ``` #### Go ```go +func countOddLetters(n int) int { + d := map[int]string{ + 0: "zero", + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine", + } + + mask := 0 + for n > 0 { + x := n % 10 + n /= 10 + for _, c := range d[x] { + mask ^= 1 << (c - 'a') + } + } + + return bits.OnesCount32(uint32(mask)) +} +``` +#### TypeScript + +```ts +function countOddLetters(n: number): number { + const d: Record = { + 0: 'zero', + 1: 'one', + 2: 'two', + 3: 'three', + 4: 'four', + 5: 'five', + 6: 'six', + 7: 'seven', + 8: 'eight', + 9: 'nine', + }; + + let mask = 0; + while (n > 0) { + const x = n % 10; + n = Math.floor(n / 10); + for (const c of d[x]) { + mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0)); + } + } + + return bitCount(mask); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} ``` diff --git a/solution/3500-3599/3581.Count Odd Letters from Number/Solution.cpp b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.cpp new file mode 100644 index 0000000000000..ad35f33be1715 --- /dev/null +++ b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int countOddLetters(int n) { + static const unordered_map d = { + {0, "zero"}, + {1, "one"}, + {2, "two"}, + {3, "three"}, + {4, "four"}, + {5, "five"}, + {6, "six"}, + {7, "seven"}, + {8, "eight"}, + {9, "nine"}}; + + int mask = 0; + while (n > 0) { + int x = n % 10; + n /= 10; + for (char c : d.at(x)) { + mask ^= 1 << (c - 'a'); + } + } + return __builtin_popcount(mask); + } +}; diff --git a/solution/3500-3599/3581.Count Odd Letters from Number/Solution.go b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.go new file mode 100644 index 0000000000000..b387ea5fbc68d --- /dev/null +++ b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.go @@ -0,0 +1,25 @@ +func countOddLetters(n int) int { + d := map[int]string{ + 0: "zero", + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine", + } + + mask := 0 + for n > 0 { + x := n % 10 + n /= 10 + for _, c := range d[x] { + mask ^= 1 << (c - 'a') + } + } + + return bits.OnesCount32(uint32(mask)) +} diff --git a/solution/3500-3599/3581.Count Odd Letters from Number/Solution.java b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.java new file mode 100644 index 0000000000000..42ea9adb09c3f --- /dev/null +++ b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.java @@ -0,0 +1,27 @@ +class Solution { + private static final Map d = new HashMap<>(); + static { + d.put(0, "zero"); + d.put(1, "one"); + d.put(2, "two"); + d.put(3, "three"); + d.put(4, "four"); + d.put(5, "five"); + d.put(6, "six"); + d.put(7, "seven"); + d.put(8, "eight"); + d.put(9, "nine"); + } + + public int countOddLetters(int n) { + int mask = 0; + while (n > 0) { + int x = n % 10; + n /= 10; + for (char c : d.get(x).toCharArray()) { + mask ^= 1 << (c - 'a'); + } + } + return Integer.bitCount(mask); + } +} diff --git a/solution/3500-3599/3581.Count Odd Letters from Number/Solution.py b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.py new file mode 100644 index 0000000000000..61a9f34408ac6 --- /dev/null +++ b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.py @@ -0,0 +1,23 @@ +d = { + 0: "zero", + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine", +} + + +class Solution: + def countOddLetters(self, n: int) -> int: + mask = 0 + while n: + x = n % 10 + n //= 10 + for c in d[x]: + mask ^= 1 << (ord(c) - ord("a")) + return mask.bit_count() diff --git a/solution/3500-3599/3581.Count Odd Letters from Number/Solution.ts b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.ts new file mode 100644 index 0000000000000..30eaa298cf0c2 --- /dev/null +++ b/solution/3500-3599/3581.Count Odd Letters from Number/Solution.ts @@ -0,0 +1,34 @@ +function countOddLetters(n: number): number { + const d: Record = { + 0: 'zero', + 1: 'one', + 2: 'two', + 3: 'three', + 4: 'four', + 5: 'five', + 6: 'six', + 7: 'seven', + 8: 'eight', + 9: 'nine', + }; + + let mask = 0; + while (n > 0) { + const x = n % 10; + n = Math.floor(n / 10); + for (const c of d[x]) { + mask ^= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0)); + } + } + + return bitCount(mask); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +}