Skip to content

Commit 2fe7396

Browse files
committed
feat: add solutions to lc problem: No.2283
No.2283.Check if Number Has Equal Digit Count and Digit Value
1 parent 69f2966 commit 2fe7396

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,56 @@ func digitCount(num string) bool {
130130
### **TypeScript**
131131

132132
```ts
133+
function digitCount(num: string): boolean {
134+
const n = num.length;
135+
const count = new Array(10).fill(0);
136+
for (let i = 0; i < n; i++) {
137+
count[i] = Number(num[i]);
138+
}
139+
for (const c of num) {
140+
count[c]--;
141+
}
142+
return count.every(v => v === 0);
143+
}
144+
```
145+
146+
### **Rust**
133147

148+
```rust
149+
impl Solution {
150+
pub fn digit_count(num: String) -> bool {
151+
let s = num.as_bytes();
152+
let n = num.len();
153+
let mut count = [0; 10];
154+
for i in 0..n {
155+
count[i] = s[i] - b'0';
156+
}
157+
for c in s {
158+
count[(c - b'0') as usize] -= 1;
159+
}
160+
count.iter().all(|v| *v == 0)
161+
}
162+
}
163+
```
164+
165+
### **C**
166+
167+
```c
168+
bool digitCount(char *num) {
169+
int count[10] = {0};
170+
for (int i = 0; num[i]; i++) {
171+
count[i] = num[i] - '0';
172+
}
173+
for (int i = 0; num[i]; i++) {
174+
count[num[i] - '0']--;
175+
}
176+
for (int i = 0; i < 10; i++) {
177+
if (count[i] != 0) {
178+
return false;
179+
}
180+
}
181+
return true;
182+
}
134183
```
135184
136185
### **...**

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,56 @@ func digitCount(num string) bool {
116116
### **TypeScript**
117117

118118
```ts
119+
function digitCount(num: string): boolean {
120+
const n = num.length;
121+
const count = new Array(10).fill(0);
122+
for (let i = 0; i < n; i++) {
123+
count[i] = Number(num[i]);
124+
}
125+
for (const c of num) {
126+
count[c]--;
127+
}
128+
return count.every(v => v === 0);
129+
}
130+
```
131+
132+
### **Rust**
119133

134+
```rust
135+
impl Solution {
136+
pub fn digit_count(num: String) -> bool {
137+
let s = num.as_bytes();
138+
let n = num.len();
139+
let mut count = [0; 10];
140+
for i in 0..n {
141+
count[i] = s[i] - b'0';
142+
}
143+
for c in s {
144+
count[(c - b'0') as usize] -= 1;
145+
}
146+
count.iter().all(|v| *v == 0)
147+
}
148+
}
149+
```
150+
151+
### **C**
152+
153+
```c
154+
bool digitCount(char *num) {
155+
int count[10] = {0};
156+
for (int i = 0; num[i]; i++) {
157+
count[i] = num[i] - '0';
158+
}
159+
for (int i = 0; num[i]; i++) {
160+
count[num[i] - '0']--;
161+
}
162+
for (int i = 0; i < 10; i++) {
163+
if (count[i] != 0) {
164+
return false;
165+
}
166+
}
167+
return true;
168+
}
120169
```
121170
122171
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
bool digitCount(char *num) {
2+
int count[10] = {0};
3+
for (int i = 0; num[i]; i++) {
4+
count[i] = num[i] - '0';
5+
}
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) {
11+
return false;
12+
}
13+
}
14+
return true;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
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';
8+
}
9+
for c in s {
10+
count[(c - b'0') as usize] -= 1;
11+
}
12+
count.iter().all(|v| *v == 0)
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
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+
}
7+
for (const c of num) {
8+
count[c]--;
9+
}
10+
return count.every(v => v === 0);
11+
}

0 commit comments

Comments
 (0)