Skip to content

Commit 8228f7c

Browse files
authored
feat: add solutions to lc problems: No.2283,2284 (doocs#3326)
1 parent d8a2132 commit 8228f7c

File tree

15 files changed

+237
-168
lines changed

15 files changed

+237
-168
lines changed

solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README.md

+30-30
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ num[2] = '0' 。数字 2 在 num 中出现了 0 次。
6969

7070
### 方法一:计数 + 枚举
7171

72-
统计字符串中每个数字出现的次数,然后枚举每个数字,判断其出现的次数是否与其值相等,若都相等则返回 `true`,否则返回 `false`
72+
我们可以用一个长度为 $10$ 的数组 $\textit{cnt}$ 统计字符串 $\textit{num}$ 中每个数字出现的次数,然后再枚举字符串 $\textit{num}$ 中的每个数字,判断其出现的次数是否等于该数字本身。如果对于所有的数字都满足这个条件,那么返回 $\text{true}$,否则返回 $\text{false}$
7373

74-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 `num` 的长度,而 $C$ 是数字的个数。本题中 $C=10$。
74+
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $\textit{num}$ 的长度,而 $|\Sigma|$ 是数字的取值范围,即 $10$。
7575

7676
<!-- tabs:start -->
7777

@@ -80,8 +80,8 @@ num[2] = '0' 。数字 2 在 num 中出现了 0 次。
8080
```python
8181
class Solution:
8282
def digitCount(self, num: str) -> bool:
83-
cnt = Counter(num)
84-
return all(cnt[str(i)] == int(v) for i, v in enumerate(num))
83+
cnt = Counter(int(x) for x in num)
84+
return all(cnt[i] == int(x) for i, x in enumerate(num))
8585
```
8686

8787
#### Java
@@ -95,7 +95,7 @@ class Solution {
9595
++cnt[num.charAt(i) - '0'];
9696
}
9797
for (int i = 0; i < n; ++i) {
98-
if (cnt[i] != num.charAt(i) - '0') {
98+
if (num.charAt(i) - '0' != cnt[i]) {
9999
return false;
100100
}
101101
}
@@ -132,8 +132,8 @@ func digitCount(num string) bool {
132132
for _, c := range num {
133133
cnt[c-'0']++
134134
}
135-
for i, v := range num {
136-
if cnt[i] != int(v-'0') {
135+
for i, c := range num {
136+
if int(c-'0') != cnt[i] {
137137
return false
138138
}
139139
}
@@ -145,15 +145,16 @@ func digitCount(num string) bool {
145145

146146
```ts
147147
function digitCount(num: string): boolean {
148-
const n = num.length;
149-
const count = new Array(10).fill(0);
150-
for (let i = 0; i < n; i++) {
151-
count[i] = Number(num[i]);
152-
}
148+
const cnt: number[] = Array(10).fill(0);
153149
for (const c of num) {
154-
count[c]--;
150+
++cnt[+c];
151+
}
152+
for (let i = 0; i < num.length; ++i) {
153+
if (cnt[i] !== +num[i]) {
154+
return false;
155+
}
155156
}
156-
return count.every(v => v === 0);
157+
return true;
157158
}
158159
```
159160

@@ -162,16 +163,18 @@ function digitCount(num: string): boolean {
162163
```rust
163164
impl Solution {
164165
pub fn digit_count(num: String) -> bool {
165-
let s = num.as_bytes();
166-
let n = num.len();
167-
let mut count = [0; 10];
168-
for i in 0..n {
169-
count[i] = s[i] - b'0';
166+
let mut cnt = vec![0; 10];
167+
for c in num.chars() {
168+
let x = c.to_digit(10).unwrap() as usize;
169+
cnt[x] += 1;
170170
}
171-
for c in s {
172-
count[(c - b'0') as usize] -= 1;
171+
for (i, c) in num.chars().enumerate() {
172+
let x = c.to_digit(10).unwrap() as usize;
173+
if cnt[i] != x {
174+
return false;
175+
}
173176
}
174-
count.iter().all(|v| *v == 0)
177+
true
175178
}
176179
}
177180
```
@@ -180,15 +183,12 @@ impl Solution {
180183

181184
```c
182185
bool digitCount(char* num) {
183-
int count[10] = {0};
184-
for (int i = 0; num[i]; i++) {
185-
count[i] = num[i] - '0';
186-
}
187-
for (int i = 0; num[i]; i++) {
188-
count[num[i] - '0']--;
186+
int cnt[10] = {0};
187+
for (int i = 0; num[i] != '\0'; ++i) {
188+
++cnt[num[i] - '0'];
189189
}
190-
for (int i = 0; i < 10; i++) {
191-
if (count[i] != 0) {
190+
for (int i = 0; num[i] != '\0'; ++i) {
191+
if (cnt[i] != num[i] - '0') {
192192
return false;
193193
}
194194
}

solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README_EN.md

+33-29
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ The indices 0 and 1 both violate the condition, so return false.
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Counting + Enumeration
69+
70+
We can use an array $\textit{cnt}$ of length $10$ to count the occurrences of each digit in the string $\textit{num}$. Then, we enumerate each digit in the string $\textit{num}$ and check if its occurrence count equals the digit itself. If this condition is satisfied for all digits, we return $\text{true}$; otherwise, we return $\text{false}$.
71+
72+
The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string $\textit{num}$, and $|\Sigma|$ is the range of possible digit values, which is $10$.
6973

7074
<!-- tabs:start -->
7175

@@ -74,8 +78,8 @@ The indices 0 and 1 both violate the condition, so return false.
7478
```python
7579
class Solution:
7680
def digitCount(self, num: str) -> bool:
77-
cnt = Counter(num)
78-
return all(cnt[str(i)] == int(v) for i, v in enumerate(num))
81+
cnt = Counter(int(x) for x in num)
82+
return all(cnt[i] == int(x) for i, x in enumerate(num))
7983
```
8084

8185
#### Java
@@ -89,7 +93,7 @@ class Solution {
8993
++cnt[num.charAt(i) - '0'];
9094
}
9195
for (int i = 0; i < n; ++i) {
92-
if (cnt[i] != num.charAt(i) - '0') {
96+
if (num.charAt(i) - '0' != cnt[i]) {
9397
return false;
9498
}
9599
}
@@ -126,8 +130,8 @@ func digitCount(num string) bool {
126130
for _, c := range num {
127131
cnt[c-'0']++
128132
}
129-
for i, v := range num {
130-
if cnt[i] != int(v-'0') {
133+
for i, c := range num {
134+
if int(c-'0') != cnt[i] {
131135
return false
132136
}
133137
}
@@ -139,15 +143,16 @@ func digitCount(num string) bool {
139143

140144
```ts
141145
function digitCount(num: string): boolean {
142-
const n = num.length;
143-
const count = new Array(10).fill(0);
144-
for (let i = 0; i < n; i++) {
145-
count[i] = Number(num[i]);
146-
}
146+
const cnt: number[] = Array(10).fill(0);
147147
for (const c of num) {
148-
count[c]--;
148+
++cnt[+c];
149+
}
150+
for (let i = 0; i < num.length; ++i) {
151+
if (cnt[i] !== +num[i]) {
152+
return false;
153+
}
149154
}
150-
return count.every(v => v === 0);
155+
return true;
151156
}
152157
```
153158

@@ -156,16 +161,18 @@ function digitCount(num: string): boolean {
156161
```rust
157162
impl Solution {
158163
pub fn digit_count(num: String) -> bool {
159-
let s = num.as_bytes();
160-
let n = num.len();
161-
let mut count = [0; 10];
162-
for i in 0..n {
163-
count[i] = s[i] - b'0';
164+
let mut cnt = vec![0; 10];
165+
for c in num.chars() {
166+
let x = c.to_digit(10).unwrap() as usize;
167+
cnt[x] += 1;
164168
}
165-
for c in s {
166-
count[(c - b'0') as usize] -= 1;
169+
for (i, c) in num.chars().enumerate() {
170+
let x = c.to_digit(10).unwrap() as usize;
171+
if cnt[i] != x {
172+
return false;
173+
}
167174
}
168-
count.iter().all(|v| *v == 0)
175+
true
169176
}
170177
}
171178
```
@@ -174,15 +181,12 @@ impl Solution {
174181

175182
```c
176183
bool digitCount(char* num) {
177-
int count[10] = {0};
178-
for (int i = 0; num[i]; i++) {
179-
count[i] = num[i] - '0';
180-
}
181-
for (int i = 0; num[i]; i++) {
182-
count[num[i] - '0']--;
184+
int cnt[10] = {0};
185+
for (int i = 0; num[i] != '\0'; ++i) {
186+
++cnt[num[i] - '0'];
183187
}
184-
for (int i = 0; i < 10; i++) {
185-
if (count[i] != 0) {
188+
for (int i = 0; num[i] != '\0'; ++i) {
189+
if (cnt[i] != num[i] - '0') {
186190
return false;
187191
}
188192
}

solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
bool digitCount(char* num) {
2-
int count[10] = {0};
3-
for (int i = 0; num[i]; i++) {
4-
count[i] = num[i] - '0';
2+
int cnt[10] = {0};
3+
for (int i = 0; num[i] != '\0'; ++i) {
4+
++cnt[num[i] - '0'];
55
}
6-
for (int i = 0; num[i]; i++) {
7-
count[num[i] - '0']--;
8-
}
9-
for (int i = 0; i < 10; i++) {
10-
if (count[i] != 0) {
6+
for (int i = 0; num[i] != '\0'; ++i) {
7+
if (cnt[i] != num[i] - '0') {
118
return false;
129
}
1310
}

solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ func digitCount(num string) bool {
33
for _, c := range num {
44
cnt[c-'0']++
55
}
6-
for i, v := range num {
7-
if cnt[i] != int(v-'0') {
6+
for i, c := range num {
7+
if int(c-'0') != cnt[i] {
88
return false
99
}
1010
}

solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public boolean digitCount(String num) {
66
++cnt[num.charAt(i) - '0'];
77
}
88
for (int i = 0; i < n; ++i) {
9-
if (cnt[i] != num.charAt(i) - '0') {
9+
if (num.charAt(i) - '0' != cnt[i]) {
1010
return false;
1111
}
1212
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Solution:
22
def digitCount(self, num: str) -> bool:
3-
cnt = Counter(num)
4-
return all(cnt[str(i)] == int(v) for i, v in enumerate(num))
3+
cnt = Counter(int(x) for x in num)
4+
return all(cnt[i] == int(x) for i, x in enumerate(num))
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
impl Solution {
22
pub fn digit_count(num: String) -> bool {
3-
let s = num.as_bytes();
4-
let n = num.len();
5-
let mut count = [0; 10];
6-
for i in 0..n {
7-
count[i] = s[i] - b'0';
3+
let mut cnt = vec![0; 10];
4+
for c in num.chars() {
5+
let x = c.to_digit(10).unwrap() as usize;
6+
cnt[x] += 1;
87
}
9-
for c in s {
10-
count[(c - b'0') as usize] -= 1;
8+
for (i, c) in num.chars().enumerate() {
9+
let x = c.to_digit(10).unwrap() as usize;
10+
if cnt[i] != x {
11+
return false;
12+
}
1113
}
12-
count.iter().all(|v| *v == 0)
14+
true
1315
}
1416
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
function digitCount(num: string): boolean {
2-
const n = num.length;
3-
const count = new Array(10).fill(0);
4-
for (let i = 0; i < n; i++) {
5-
count[i] = Number(num[i]);
6-
}
2+
const cnt: number[] = Array(10).fill(0);
73
for (const c of num) {
8-
count[c]--;
4+
++cnt[+c];
5+
}
6+
for (let i = 0; i < num.length; ++i) {
7+
if (cnt[i] !== +num[i]) {
8+
return false;
9+
}
910
}
10-
return count.every(v => v === 0);
11+
return true;
1112
}

0 commit comments

Comments
 (0)