Skip to content

Commit da7e70c

Browse files
committed
feat: add solutions to lc problem: No.2114
No.2114.Maximum Number of Words Found in Sentences
1 parent 7029592 commit da7e70c

File tree

7 files changed

+59
-133
lines changed

7 files changed

+59
-133
lines changed

solution/2100-2199/2114.Maximum Number of Words Found in Sentences/README.md

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
返回最大空格数量再加 1 即可。
52+
**方法一:空格计数**
53+
54+
我们遍历数组 `sentences`,对于每个句子,我们计算其中的空格数,那么单词数就是空格数加 $1$。最后返回最大的单词数即可。
55+
56+
时间复杂度 $O(L)$,空间复杂度 $O(1)$。其中 $L$ 是数组 `sentences` 中所有字符串的长度之和。
5357

5458
<!-- tabs:start -->
5559

@@ -69,23 +73,18 @@ class Solution:
6973

7074
```java
7175
class Solution {
72-
7376
public int mostWordsFound(String[] sentences) {
7477
int ans = 0;
75-
for (String s : sentences) {
76-
ans = Math.max(ans, 1 + count(s, ' '));
77-
}
78-
return ans;
79-
}
80-
81-
private int count(String s, char c) {
82-
int cnt = 0;
83-
for (char ch : s.toCharArray()) {
84-
if (ch == c) {
85-
++cnt;
78+
for (var s : sentences) {
79+
int cnt = 1;
80+
for (int i = 0; i < s.length(); ++i) {
81+
if (s.charAt(i) == ' ') {
82+
++cnt;
83+
}
8684
}
85+
ans = Math.max(ans, cnt);
8786
}
88-
return cnt;
87+
return ans;
8988
}
9089
}
9190
```
@@ -97,46 +96,26 @@ class Solution {
9796
public:
9897
int mostWordsFound(vector<string>& sentences) {
9998
int ans = 0;
100-
for (string& s : sentences)
101-
ans = max(ans, 1 + count(s, ' '));
99+
for (auto& s : sentences) {
100+
int cnt = 1 + count(s.begin(), s.end(), ' ');
101+
ans = max(ans, cnt);
102+
}
102103
return ans;
103104
}
104-
105-
int count(string s, char c) {
106-
int cnt = 0;
107-
for (char& ch : s)
108-
if (ch == c)
109-
++cnt;
110-
return cnt;
111-
}
112105
};
113106
```
114107
115108
### **Go**
116109
117110
```go
118-
func mostWordsFound(sentences []string) int {
119-
count := func(s string, c rune) int {
120-
cnt := 0
121-
for _, ch := range s {
122-
if ch == c {
123-
cnt++
124-
}
125-
}
126-
return cnt
127-
}
128-
ans := 0
111+
func mostWordsFound(sentences []string) (ans int) {
129112
for _, s := range sentences {
130-
ans = max(ans, 1+count(s, ' '))
131-
}
132-
return ans
133-
}
134-
135-
func max(a, b int) int {
136-
if a > b {
137-
return a
113+
cnt := 1 + strings.Count(s, " ")
114+
if ans < cnt {
115+
ans = cnt
116+
}
138117
}
139-
return b
118+
return
140119
}
141120
```
142121

solution/2100-2199/2114.Maximum Number of Words Found in Sentences/README_EN.md

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,18 @@ class Solution:
5959

6060
```java
6161
class Solution {
62-
6362
public int mostWordsFound(String[] sentences) {
6463
int ans = 0;
65-
for (String s : sentences) {
66-
ans = Math.max(ans, 1 + count(s, ' '));
67-
}
68-
return ans;
69-
}
70-
71-
private int count(String s, char c) {
72-
int cnt = 0;
73-
for (char ch : s.toCharArray()) {
74-
if (ch == c) {
75-
++cnt;
64+
for (var s : sentences) {
65+
int cnt = 1;
66+
for (int i = 0; i < s.length(); ++i) {
67+
if (s.charAt(i) == ' ') {
68+
++cnt;
69+
}
7670
}
71+
ans = Math.max(ans, cnt);
7772
}
78-
return cnt;
73+
return ans;
7974
}
8075
}
8176
```
@@ -87,46 +82,26 @@ class Solution {
8782
public:
8883
int mostWordsFound(vector<string>& sentences) {
8984
int ans = 0;
90-
for (string& s : sentences)
91-
ans = max(ans, 1 + count(s, ' '));
85+
for (auto& s : sentences) {
86+
int cnt = 1 + count(s.begin(), s.end(), ' ');
87+
ans = max(ans, cnt);
88+
}
9289
return ans;
9390
}
94-
95-
int count(string s, char c) {
96-
int cnt = 0;
97-
for (char& ch : s)
98-
if (ch == c)
99-
++cnt;
100-
return cnt;
101-
}
10291
};
10392
```
10493
10594
### **Go**
10695
10796
```go
108-
func mostWordsFound(sentences []string) int {
109-
count := func(s string, c rune) int {
110-
cnt := 0
111-
for _, ch := range s {
112-
if ch == c {
113-
cnt++
114-
}
115-
}
116-
return cnt
117-
}
118-
ans := 0
97+
func mostWordsFound(sentences []string) (ans int) {
11998
for _, s := range sentences {
120-
ans = max(ans, 1+count(s, ' '))
121-
}
122-
return ans
123-
}
124-
125-
func max(a, b int) int {
126-
if a > b {
127-
return a
99+
cnt := 1 + strings.Count(s, " ")
100+
if ans < cnt {
101+
ans = cnt
102+
}
128103
}
129-
return b
104+
return
130105
}
131106
```
132107

solution/2100-2199/2114.Maximum Number of Words Found in Sentences/Solution.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@ class Solution {
22
public:
33
int mostWordsFound(vector<string>& sentences) {
44
int ans = 0;
5-
for (string& s : sentences)
6-
ans = max(ans, 1 + count(s, ' '));
5+
for (auto& s : sentences) {
6+
int cnt = 1 + count(s.begin(), s.end(), ' ');
7+
ans = max(ans, cnt);
8+
}
79
return ans;
810
}
9-
10-
int count(string s, char c) {
11-
int cnt = 0;
12-
for (char& ch : s)
13-
if (ch == c)
14-
++cnt;
15-
return cnt;
16-
}
1711
};
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
func mostWordsFound(sentences []string) int {
2-
count := func(s string, c rune) int {
3-
cnt := 0
4-
for _, ch := range s {
5-
if ch == c {
6-
cnt++
7-
}
8-
}
9-
return cnt
10-
}
11-
ans := 0
1+
func mostWordsFound(sentences []string) (ans int) {
122
for _, s := range sentences {
13-
ans = max(ans, 1+count(s, ' '))
14-
}
15-
return ans
16-
}
17-
18-
func max(a, b int) int {
19-
if a > b {
20-
return a
3+
cnt := 1 + strings.Count(s, " ")
4+
if ans < cnt {
5+
ans = cnt
6+
}
217
}
22-
return b
8+
return
239
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
class Solution {
22
public int mostWordsFound(String[] sentences) {
33
int ans = 0;
4-
for (String s : sentences) {
5-
ans = Math.max(ans, 1 + count(s, ' '));
6-
}
7-
return ans;
8-
}
9-
10-
private int count(String s, char c) {
11-
int cnt = 0;
12-
for (char ch : s.toCharArray()) {
13-
if (ch == c) {
14-
++cnt;
4+
for (var s : sentences) {
5+
int cnt = 1;
6+
for (int i = 0; i < s.length(); ++i) {
7+
if (s.charAt(i) == ' ') {
8+
++cnt;
9+
}
1510
}
11+
ans = Math.max(ans, cnt);
1612
}
17-
return cnt;
13+
return ans;
1814
}
1915
}

solution/2100-2199/2117.Abbreviating the Product of a Range/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
<ol>
1414
<li>统计乘积中&nbsp;<strong>后缀</strong> 0 的数目,并 <strong>移除</strong> 这些 0 ,将这个数目记为&nbsp;<code>C</code>&nbsp;。
15-
1615
<ul>
1716
<li>比方说,<code>1000</code>&nbsp;中有 <code>3</code> 个后缀 0&nbsp;,<code>546</code>&nbsp;中没有后缀 0 。</li>
1817
</ul>
@@ -27,7 +26,6 @@
2726
<li>比方说,<code>12345678987600000</code>&nbsp;被表示为&nbsp;<code>"12345...89876e5"</code>&nbsp;。</li>
2827
</ul>
2928
</li>
30-
3129
</ol>
3230

3331
<p>请你返回一个字符串,表示 <strong>闭区间</strong>&nbsp;<code>[left, right]</code>&nbsp;中所有整数&nbsp;<strong>乘积</strong>&nbsp;&nbsp;<strong>缩写</strong>&nbsp;。</p>

solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ol>
1212
<li>Count all <strong>trailing</strong> zeros in the product and <strong>remove</strong> them. Let us denote this count as <code>C</code>.
13-
1413
<ul>
1514
<li>For example, there are <code>3</code> trailing zeros in <code>1000</code>, and there are <code>0</code> trailing zeros in <code>546</code>.</li>
1615
</ul>
@@ -25,7 +24,6 @@
2524
<li>For example, <code>12345678987600000</code> will be represented as <code>&quot;12345...89876e5&quot;</code>.</li>
2625
</ul>
2726
</li>
28-
2927
</ol>
3028

3129
<p>Return <em>a string denoting the <strong>abbreviated product</strong> of all integers in the <strong>inclusive</strong> range</em> <code>[left, right]</code>.</p>

0 commit comments

Comments
 (0)