Skip to content

Commit 460a8f2

Browse files
committed
feat: add solutions to lc problems: No.2283~2285
* No.2283.Check if Number Has Equal Digit Count and Digit Value * No.2284.Sender With Largest Word Count * No.2285.Maximum Total Importance of Roads
1 parent 3e3b7e2 commit 460a8f2

File tree

18 files changed

+528
-6
lines changed

18 files changed

+528
-6
lines changed

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

+54-1
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,68 @@ num[2] = '0' 。数字 2 在 num 中出现了 0 次。
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```python
60-
60+
class Solution:
61+
def digitCount(self, num: str) -> bool:
62+
cnt = Counter(num)
63+
return all(int(v) == cnt[str(i)] for i, v in enumerate(num))
6164
```
6265

6366
### **Java**
6467

6568
<!-- 这里可写当前语言的特殊实现逻辑 -->
6669

6770
```java
71+
class Solution {
72+
public boolean digitCount(String num) {
73+
int[] cnt = new int[10];
74+
for (char c : num.toCharArray()) {
75+
++cnt[c - '0'];
76+
}
77+
for (int i = 0; i < num.length(); ++i) {
78+
int v = num.charAt(i) - '0';
79+
if (cnt[i] != v) {
80+
return false;
81+
}
82+
}
83+
return true;
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
bool digitCount(string num) {
94+
vector<int> cnt(10);
95+
for (char& c : num) ++cnt[c - '0'];
96+
for (int i = 0; i < num.size(); ++i)
97+
{
98+
int v = num[i] - '0';
99+
if (cnt[i] != v) return false;
100+
}
101+
return true;
102+
}
103+
};
104+
```
68105
106+
### **Go**
107+
108+
```go
109+
func digitCount(num string) bool {
110+
cnt := make([]int, 10)
111+
for _, c := range num {
112+
cnt[c-'0']++
113+
}
114+
for i, c := range num {
115+
v := int(c - '0')
116+
if cnt[i] != v {
117+
return false
118+
}
119+
}
120+
return true
121+
}
69122
```
70123

71124
### **TypeScript**

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

+54-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,66 @@ The indices 0 and 1 both violate the condition, so return false.
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def digitCount(self, num: str) -> bool:
56+
cnt = Counter(num)
57+
return all(int(v) == cnt[str(i)] for i, v in enumerate(num))
5558
```
5659

5760
### **Java**
5861

5962
```java
63+
class Solution {
64+
public boolean digitCount(String num) {
65+
int[] cnt = new int[10];
66+
for (char c : num.toCharArray()) {
67+
++cnt[c - '0'];
68+
}
69+
for (int i = 0; i < num.length(); ++i) {
70+
int v = num.charAt(i) - '0';
71+
if (cnt[i] != v) {
72+
return false;
73+
}
74+
}
75+
return true;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
bool digitCount(string num) {
86+
vector<int> cnt(10);
87+
for (char& c : num) ++cnt[c - '0'];
88+
for (int i = 0; i < num.size(); ++i)
89+
{
90+
int v = num[i] - '0';
91+
if (cnt[i] != v) return false;
92+
}
93+
return true;
94+
}
95+
};
96+
```
6097
98+
### **Go**
99+
100+
```go
101+
func digitCount(num string) bool {
102+
cnt := make([]int, 10)
103+
for _, c := range num {
104+
cnt[c-'0']++
105+
}
106+
for i, c := range num {
107+
v := int(c - '0')
108+
if cnt[i] != v {
109+
return false
110+
}
111+
}
112+
return true
113+
}
61114
```
62115

63116
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool digitCount(string num) {
4+
vector<int> cnt(10);
5+
for (char& c : num) ++cnt[c - '0'];
6+
for (int i = 0; i < num.size(); ++i)
7+
{
8+
int v = num[i] - '0';
9+
if (cnt[i] != v) return false;
10+
}
11+
return true;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func digitCount(num string) bool {
2+
cnt := make([]int, 10)
3+
for _, c := range num {
4+
cnt[c-'0']++
5+
}
6+
for i, c := range num {
7+
v := int(c - '0')
8+
if cnt[i] != v {
9+
return false
10+
}
11+
}
12+
return true
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean digitCount(String num) {
3+
int[] cnt = new int[10];
4+
for (char c : num.toCharArray()) {
5+
++cnt[c - '0'];
6+
}
7+
for (int i = 0; i < num.length(); ++i) {
8+
int v = num.charAt(i) - '0';
9+
if (cnt[i] != v) {
10+
return false;
11+
}
12+
}
13+
return true;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def digitCount(self, num: str) -> bool:
3+
cnt = Counter(num)
4+
return all(int(v) == cnt[str(i)] for i, v in enumerate(num))

solution/2200-2299/2284.Sender With Largest Word Count/README.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,83 @@ Charlie 总共发出了 5 个单词。
6666
<!-- 这里可写当前语言的特殊实现逻辑 -->
6767

6868
```python
69-
69+
class Solution:
70+
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
71+
cnt = Counter()
72+
for m, s in zip(messages, senders):
73+
cnt[s] += m.count(' ') + 1
74+
return sorted(cnt.items(), key=lambda x: (x[1], x[0]))[-1][0]
7075
```
7176

7277
### **Java**
7378

7479
<!-- 这里可写当前语言的特殊实现逻辑 -->
7580

7681
```java
82+
class Solution {
83+
public String largestWordCount(String[] messages, String[] senders) {
84+
Map<String, Integer> cnt = new HashMap<>();
85+
int n = senders.length;
86+
for (int i = 0; i < n; ++i) {
87+
cnt.put(senders[i], cnt.getOrDefault(senders[i], 0) + messages[i].split(" ").length);
88+
}
89+
String ans = senders[0];
90+
for (Map.Entry<String, Integer> e : cnt.entrySet()) {
91+
String u = e.getKey();
92+
int v = e.getValue();
93+
if (v > cnt.get(ans) || (v == cnt.get(ans) && ans.compareTo(u) < 0)) {
94+
ans = u;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
string largestWordCount(vector<string>& messages, vector<string>& senders) {
108+
unordered_map<string, int> cnt;
109+
int n = senders.size();
110+
for (int i = 0; i < n; ++i)
111+
{
112+
int v = 0;
113+
for (char& c : messages[i])
114+
{
115+
if (c == ' ') ++v;
116+
}
117+
cnt[senders[i]] += v + 1;
118+
}
119+
string ans = senders[0];
120+
for (auto& [u, v] : cnt)
121+
{
122+
if (v > cnt[ans] || (v == cnt[ans] && u > ans)) ans = u;
123+
}
124+
return ans;
125+
}
126+
};
127+
```
77128
129+
### **Go**
130+
131+
```go
132+
func largestWordCount(messages []string, senders []string) string {
133+
cnt := map[string]int{}
134+
for i, msg := range messages {
135+
v := strings.Count(msg, " ") + 1
136+
cnt[senders[i]] += v
137+
}
138+
ans := ""
139+
for u, v := range cnt {
140+
if v > cnt[ans] || (v == cnt[ans] && u > ans) {
141+
ans = u
142+
}
143+
}
144+
return ans
145+
}
78146
```
79147

80148
### **TypeScript**

solution/2200-2299/2284.Sender With Largest Word Count/README_EN.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,81 @@ Since there is a tie for the largest word count, we return the sender with the l
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
65+
cnt = Counter()
66+
for m, s in zip(messages, senders):
67+
cnt[s] += m.count(' ') + 1
68+
return sorted(cnt.items(), key=lambda x: (x[1], x[0]))[-1][0]
6469
```
6570

6671
### **Java**
6772

6873
```java
74+
class Solution {
75+
public String largestWordCount(String[] messages, String[] senders) {
76+
Map<String, Integer> cnt = new HashMap<>();
77+
int n = senders.length;
78+
for (int i = 0; i < n; ++i) {
79+
cnt.put(senders[i], cnt.getOrDefault(senders[i], 0) + messages[i].split(" ").length);
80+
}
81+
String ans = senders[0];
82+
for (Map.Entry<String, Integer> e : cnt.entrySet()) {
83+
String u = e.getKey();
84+
int v = e.getValue();
85+
if (v > cnt.get(ans) || (v == cnt.get(ans) && ans.compareTo(u) < 0)) {
86+
ans = u;
87+
}
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
string largestWordCount(vector<string>& messages, vector<string>& senders) {
100+
unordered_map<string, int> cnt;
101+
int n = senders.size();
102+
for (int i = 0; i < n; ++i)
103+
{
104+
int v = 0;
105+
for (char& c : messages[i])
106+
{
107+
if (c == ' ') ++v;
108+
}
109+
cnt[senders[i]] += v + 1;
110+
}
111+
string ans = senders[0];
112+
for (auto& [u, v] : cnt)
113+
{
114+
if (v > cnt[ans] || (v == cnt[ans] && u > ans)) ans = u;
115+
}
116+
return ans;
117+
}
118+
};
119+
```
69120
121+
### **Go**
122+
123+
```go
124+
func largestWordCount(messages []string, senders []string) string {
125+
cnt := map[string]int{}
126+
for i, msg := range messages {
127+
v := strings.Count(msg, " ") + 1
128+
cnt[senders[i]] += v
129+
}
130+
ans := ""
131+
for u, v := range cnt {
132+
if v > cnt[ans] || (v == cnt[ans] && u > ans) {
133+
ans = u
134+
}
135+
}
136+
return ans
137+
}
70138
```
71139

72140
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
string largestWordCount(vector<string>& messages, vector<string>& senders) {
4+
unordered_map<string, int> cnt;
5+
int n = senders.size();
6+
for (int i = 0; i < n; ++i)
7+
{
8+
int v = 0;
9+
for (char& c : messages[i])
10+
{
11+
if (c == ' ') ++v;
12+
}
13+
cnt[senders[i]] += v + 1;
14+
}
15+
string ans = senders[0];
16+
for (auto& [u, v] : cnt)
17+
{
18+
if (v > cnt[ans] || (v == cnt[ans] && u > ans)) ans = u;
19+
}
20+
return ans;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func largestWordCount(messages []string, senders []string) string {
2+
cnt := map[string]int{}
3+
for i, msg := range messages {
4+
v := strings.Count(msg, " ") + 1
5+
cnt[senders[i]] += v
6+
}
7+
ans := ""
8+
for u, v := range cnt {
9+
if v > cnt[ans] || (v == cnt[ans] && u > ans) {
10+
ans = u
11+
}
12+
}
13+
return ans
14+
}

0 commit comments

Comments
 (0)