Skip to content

Commit bbfed52

Browse files
committed
feat: add solutions to lc problem: No.1773
No.1773.Count Items Matching a Rule
1 parent ca5cef3 commit bbfed52

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

solution/1700-1799/1773.Count Items Matching a Rule/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,51 @@ func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int)
116116
}
117117
```
118118

119+
### **C**
120+
121+
```c
122+
int countMatches(char ***items, int itemsSize, int *itemsColSize, char *ruleKey, char *ruleValue) {
123+
int k = strcmp(ruleKey, "type") == 0 ? 0 : strcmp(ruleKey, "color") == 0 ? 1 : 2;
124+
int res = 0;
125+
for (int i = 0; i < itemsSize; i++) {
126+
if (strcmp(items[i][k], ruleValue) == 0) {
127+
res++;
128+
}
129+
}
130+
return res;
131+
}
132+
```
133+
134+
### **TypeScript**
135+
136+
```ts
137+
function countMatches(
138+
items: string[][],
139+
ruleKey: string,
140+
ruleValue: string,
141+
): number {
142+
const key = ruleKey === 'type' ? 0 : ruleKey === 'color' ? 1 : 2;
143+
return items.reduce((r, v) => r + (v[key] === ruleValue ? 1 : 0), 0);
144+
}
145+
```
146+
147+
### **Rust**
148+
149+
```rust
150+
impl Solution {
151+
pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
152+
let key = if rule_key == "type" {
153+
0
154+
} else if rule_key == "color" {
155+
1
156+
} else {
157+
2
158+
};
159+
items.iter().filter(|v| v[key] == rule_value).count() as i32
160+
}
161+
}
162+
```
163+
119164
### **...**
120165

121166
```

solution/1700-1799/1773.Count Items Matching a Rule/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,51 @@ func countMatches(items [][]string, ruleKey string, ruleValue string) (ans int)
9898
}
9999
```
100100

101+
### **C**
102+
103+
```c
104+
int countMatches(char ***items, int itemsSize, int *itemsColSize, char *ruleKey, char *ruleValue) {
105+
int k = strcmp(ruleKey, "type") == 0 ? 0 : strcmp(ruleKey, "color") == 0 ? 1 : 2;
106+
int res = 0;
107+
for (int i = 0; i < itemsSize; i++) {
108+
if (strcmp(items[i][k], ruleValue) == 0) {
109+
res++;
110+
}
111+
}
112+
return res;
113+
}
114+
```
115+
116+
### **TypeScript**
117+
118+
```ts
119+
function countMatches(
120+
items: string[][],
121+
ruleKey: string,
122+
ruleValue: string,
123+
): number {
124+
const key = ruleKey === 'type' ? 0 : ruleKey === 'color' ? 1 : 2;
125+
return items.reduce((r, v) => r + (v[key] === ruleValue ? 1 : 0), 0);
126+
}
127+
```
128+
129+
### **Rust**
130+
131+
```rust
132+
impl Solution {
133+
pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
134+
let key = if rule_key == "type" {
135+
0
136+
} else if rule_key == "color" {
137+
1
138+
} else {
139+
2
140+
};
141+
items.iter().filter(|v| v[key] == rule_value).count() as i32
142+
}
143+
}
144+
```
145+
101146
### **...**
102147

103148
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int countMatches(char ***items, int itemsSize, int *itemsColSize, char *ruleKey, char *ruleValue) {
2+
int k = strcmp(ruleKey, "type") == 0 ? 0 : strcmp(ruleKey, "color") == 0 ? 1 : 2;
3+
int res = 0;
4+
for (int i = 0; i < itemsSize; i++) {
5+
if (strcmp(items[i][k], ruleValue) == 0) {
6+
res++;
7+
}
8+
}
9+
return res;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn count_matches(items: Vec<Vec<String>>, rule_key: String, rule_value: String) -> i32 {
3+
let key = if rule_key == "type" {
4+
0
5+
} else if rule_key == "color" {
6+
1
7+
} else {
8+
2
9+
};
10+
items.iter().filter(|v| v[key] == rule_value).count() as i32
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function countMatches(
2+
items: string[][],
3+
ruleKey: string,
4+
ruleValue: string,
5+
): number {
6+
const key = ruleKey === 'type' ? 0 : ruleKey === 'color' ? 1 : 2;
7+
return items.reduce((r, v) => r + (v[key] === ruleValue ? 1 : 0), 0);
8+
}

0 commit comments

Comments
 (0)