Skip to content

feat: add solutions to lc problem: No.3581 #4501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 144 additions & 4 deletions solution/3500-3599/3581.Count Odd Letters from Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3581.Co

<!-- solution:start -->

### 方法一
### 方法一:模拟 + 位运算

我们可以将每个数字转换为对应的英文单词,然后统计每个字母出现的次数。由于字母的数量有限,我们可以使用一个整数 $\textit{mask}$ 来表示每个字母的出现情况。具体地,我们可以将字母映射到整数的二进制位上,如果某个字母出现了奇数次,则对应的二进制位为 1,否则为 0。最后,我们只需要统计 $\textit{mask}$ 中为 1 的位数,即为答案。

时间复杂度 $O(\log n)$,其中 $n$ 是输入的整数。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### 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<Integer, String> 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<int, string> 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<number, string> = {
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;
}
```

<!-- tabs:end -->
Expand Down
148 changes: 144 additions & 4 deletions solution/3500-3599/3581.Count Odd Letters from Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,172 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3581.Co

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

#### 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<Integer, String> 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<int, string> 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<number, string> = {
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;
}
```

<!-- tabs:end -->
Expand Down
26 changes: 26 additions & 0 deletions solution/3500-3599/3581.Count Odd Letters from Number/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int countOddLetters(int n) {
static const unordered_map<int, string> 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);
}
};
Loading