Skip to content

Commit 95cc2c2

Browse files
authored
feat: add solutions to lc problems: No.2255,2256 (#1717)
* No.2255.Count Prefixes of a Given String * No.2256.Minimum Average Difference
1 parent 4af7f8a commit 95cc2c2

File tree

14 files changed

+293
-199
lines changed

14 files changed

+293
-199
lines changed

solution/2200-2299/2255.Count Prefixes of a Given String/README.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ words 中是 s = "abc" 前缀的字符串为:
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:遍历计数**
49+
50+
我们直接遍历数组 $words$,对于每个字符串 $w$,判断 $s$ 是否以 $w$ 为前缀,如果是则答案加一。
51+
52+
遍历结束后,返回答案即可。
53+
54+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是数组 $words$ 的长度和字符串 $s$ 的长度。空间复杂度 $O(1)$。
55+
4856
<!-- tabs:start -->
4957

5058
### **Python3**
@@ -54,7 +62,7 @@ words 中是 s = "abc" 前缀的字符串为:
5462
```python
5563
class Solution:
5664
def countPrefixes(self, words: List[str], s: str) -> int:
57-
return sum(word == s[: len(word)] for word in words)
65+
return sum(s.startswith(w) for w in words)
5866
```
5967

6068
### **Java**
@@ -65,8 +73,8 @@ class Solution:
6573
class Solution {
6674
public int countPrefixes(String[] words, String s) {
6775
int ans = 0;
68-
for (String word : words) {
69-
if (word.equals(s.substring(0, Math.min(s.length(), word.length())))) {
76+
for (String w : words) {
77+
if (s.startsWith(w)) {
7078
++ans;
7179
}
7280
}
@@ -82,9 +90,9 @@ class Solution {
8290
public:
8391
int countPrefixes(vector<string>& words, string s) {
8492
int ans = 0;
85-
for (auto& word : words)
86-
if (s.substr(0, word.size()) == word)
87-
++ans;
93+
for (auto& w : words) {
94+
ans += s.starts_with(w);
95+
}
8896
return ans;
8997
}
9098
};
@@ -93,21 +101,22 @@ public:
93101
### **Go**
94102
95103
```go
96-
func countPrefixes(words []string, s string) int {
97-
ans := 0
98-
for _, word := range words {
99-
if strings.HasPrefix(s, word) {
104+
func countPrefixes(words []string, s string) (ans int) {
105+
for _, w := range words {
106+
if strings.HasPrefix(s, w) {
100107
ans++
101108
}
102109
}
103-
return ans
110+
return
104111
}
105112
```
106113

107114
### **TypeScript**
108115

109116
```ts
110-
117+
function countPrefixes(words: string[], s: string): number {
118+
return words.filter(w => s.startsWith(w)).length;
119+
}
111120
```
112121

113122
### **...**

solution/2200-2299/2255.Count Prefixes of a Given String/README_EN.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ Note that the same string can occur multiple times in words, and it should be co
4141

4242
## Solutions
4343

44+
**Solution 1: Traversal Counting**
45+
46+
We directly traverse the array words, and for each string w, we check if s starts with w as a prefix. If it does, we increment the answer by one.
47+
48+
After the traversal, we return the answer.
49+
50+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the lengths of the array words and the string s, respectively. The space complexity is $O(1)$.
51+
4452
<!-- tabs:start -->
4553

4654
### **Python3**
4755

4856
```python
4957
class Solution:
5058
def countPrefixes(self, words: List[str], s: str) -> int:
51-
return sum(word == s[: len(word)] for word in words)
59+
return sum(s.startswith(w) for w in words)
5260
```
5361

5462
### **Java**
@@ -57,8 +65,8 @@ class Solution:
5765
class Solution {
5866
public int countPrefixes(String[] words, String s) {
5967
int ans = 0;
60-
for (String word : words) {
61-
if (word.equals(s.substring(0, Math.min(s.length(), word.length())))) {
68+
for (String w : words) {
69+
if (s.startsWith(w)) {
6270
++ans;
6371
}
6472
}
@@ -74,9 +82,9 @@ class Solution {
7482
public:
7583
int countPrefixes(vector<string>& words, string s) {
7684
int ans = 0;
77-
for (auto& word : words)
78-
if (s.substr(0, word.size()) == word)
79-
++ans;
85+
for (auto& w : words) {
86+
ans += s.starts_with(w);
87+
}
8088
return ans;
8189
}
8290
};
@@ -85,21 +93,22 @@ public:
8593
### **Go**
8694
8795
```go
88-
func countPrefixes(words []string, s string) int {
89-
ans := 0
90-
for _, word := range words {
91-
if strings.HasPrefix(s, word) {
96+
func countPrefixes(words []string, s string) (ans int) {
97+
for _, w := range words {
98+
if strings.HasPrefix(s, w) {
9299
ans++
93100
}
94101
}
95-
return ans
102+
return
96103
}
97104
```
98105

99106
### **TypeScript**
100107

101108
```ts
102-
109+
function countPrefixes(words: string[], s: string): number {
110+
return words.filter(w => s.startsWith(w)).length;
111+
}
103112
```
104113

105114
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
class Solution {
2-
public:
3-
int countPrefixes(vector<string>& words, string s) {
4-
int ans = 0;
5-
for (auto& word : words)
6-
if (s.substr(0, word.size()) == word)
7-
++ans;
8-
return ans;
9-
}
1+
class Solution {
2+
public:
3+
int countPrefixes(vector<string>& words, string s) {
4+
int ans = 0;
5+
for (auto& w : words) {
6+
ans += s.starts_with(w);
7+
}
8+
return ans;
9+
}
1010
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
func countPrefixes(words []string, s string) int {
2-
ans := 0
3-
for _, word := range words {
4-
if strings.HasPrefix(s, word) {
1+
func countPrefixes(words []string, s string) (ans int) {
2+
for _, w := range words {
3+
if strings.HasPrefix(s, w) {
54
ans++
65
}
76
}
8-
return ans
7+
return
98
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
class Solution {
2-
public int countPrefixes(String[] words, String s) {
3-
int ans = 0;
4-
for (String word : words) {
5-
if (word.equals(s.substring(0, Math.min(s.length(), word.length())))) {
6-
++ans;
7-
}
8-
}
9-
return ans;
10-
}
1+
class Solution {
2+
public int countPrefixes(String[] words, String s) {
3+
int ans = 0;
4+
for (String w : words) {
5+
if (s.startsWith(w)) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
class Solution:
2-
def countPrefixes(self, words: List[str], s: str) -> int:
3-
return sum(word == s[: len(word)] for word in words)
1+
class Solution:
2+
def countPrefixes(self, words: List[str], s: str) -> int:
3+
return sum(s.startswith(w) for w in words)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function countPrefixes(words: string[], s: string): number {
2+
return words.filter(w => s.startsWith(w)).length;
3+
}

solution/2200-2299/2256.Minimum Average Difference/README.md

+66-39
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:遍历**
62+
63+
我们直接遍历数组 $nums$,对于每个下标 $i$,维护前 $i + 1$ 个元素的和 $pre$ 和后 $n - i - 1$ 个元素的和 $suf$,计算平均差的绝对值 $t$,如果 $t$ 小于当前最小值 $mi$,则更新答案 $ans = i$ 和最小值 $mi = t$。
64+
65+
遍历结束后,返回答案即可。
66+
67+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
@@ -67,14 +75,15 @@
6775
```python
6876
class Solution:
6977
def minimumAverageDifference(self, nums: List[int]) -> int:
70-
s = list(accumulate(nums))
71-
ans, n = 0, len(nums)
72-
mi = inf
73-
for i in range(n):
74-
a = s[i] // (i + 1)
75-
b = 0 if i == n - 1 else (s[-1] - s[i]) // (n - i - 1)
76-
t = abs(a - b)
77-
if mi > t:
78+
pre, suf = 0, sum(nums)
79+
n = len(nums)
80+
ans, mi = 0, inf
81+
for i, x in enumerate(nums):
82+
pre += x
83+
suf -= x
84+
a = pre // (i + 1)
85+
b = 0 if n - i - 1 == 0 else suf // (n - i - 1)
86+
if (t := abs(a - b)) < mi:
7887
ans = i
7988
mi = t
8089
return ans
@@ -88,18 +97,19 @@ class Solution:
8897
class Solution {
8998
public int minimumAverageDifference(int[] nums) {
9099
int n = nums.length;
91-
long[] s = new long[n];
92-
s[0] = nums[0];
93-
for (int i = 1; i < n; ++i) {
94-
s[i] = s[i - 1] + nums[i];
100+
long pre = 0, suf = 0;
101+
for (int x : nums) {
102+
suf += x;
95103
}
96104
int ans = 0;
97105
long mi = Long.MAX_VALUE;
98106
for (int i = 0; i < n; ++i) {
99-
long a = s[i] / (i + 1);
100-
long b = i == n - 1 ? 0 : (s[n - 1] - s[i]) / (n - i - 1);
107+
pre += nums[i];
108+
suf -= nums[i];
109+
long a = pre / (i + 1);
110+
long b = n - i - 1 == 0 ? 0 : suf / (n - i - 1);
101111
long t = Math.abs(a - b);
102-
if (mi > t) {
112+
if (t < mi) {
103113
ans = i;
104114
mi = t;
105115
}
@@ -112,22 +122,22 @@ class Solution {
112122
### **C++**
113123

114124
```cpp
115-
typedef long long ll;
116-
117125
class Solution {
118126
public:
119127
int minimumAverageDifference(vector<int>& nums) {
120128
int n = nums.size();
121-
vector<ll> s(n);
122-
s[0] = nums[0];
123-
for (int i = 1; i < n; ++i) s[i] = s[i - 1] + nums[i];
129+
using ll = long long;
130+
ll pre = 0;
131+
ll suf = accumulate(nums.begin(), nums.end(), 0LL);
124132
int ans = 0;
125-
ll mi = LONG_MAX;
133+
ll mi = suf;
126134
for (int i = 0; i < n; ++i) {
127-
ll a = s[i] / (i + 1);
128-
ll b = i == n - 1 ? 0 : (s[n - 1] - s[i]) / (n - i - 1);
135+
pre += nums[i];
136+
suf -= nums[i];
137+
ll a = pre / (i + 1);
138+
ll b = n - i - 1 == 0 ? 0 : suf / (n - i - 1);
129139
ll t = abs(a - b);
130-
if (mi > t) {
140+
if (t < mi) {
131141
ans = i;
132142
mi = t;
133143
}
@@ -140,28 +150,27 @@ public:
140150
### **Go**
141151
142152
```go
143-
func minimumAverageDifference(nums []int) int {
153+
func minimumAverageDifference(nums []int) (ans int) {
144154
n := len(nums)
145-
s := make([]int, n)
146-
s[0] = nums[0]
147-
for i := 1; i < n; i++ {
148-
s[i] = s[i-1] + nums[i]
155+
pre, suf := 0, 0
156+
for _, x := range nums {
157+
suf += x
149158
}
150-
ans := 0
151-
mi := math.MaxInt32
152-
for i := 0; i < n; i++ {
153-
a := s[i] / (i + 1)
159+
mi := suf
160+
for i, x := range nums {
161+
pre += x
162+
suf -= x
163+
a := pre / (i + 1)
154164
b := 0
155-
if i != n-1 {
156-
b = (s[n-1] - s[i]) / (n - i - 1)
165+
if n-i-1 != 0 {
166+
b = suf / (n - i - 1)
157167
}
158-
t := abs(a - b)
159-
if mi > t {
168+
if t := abs(a - b); t < mi {
160169
ans = i
161170
mi = t
162171
}
163172
}
164-
return ans
173+
return
165174
}
166175
167176
func abs(x int) int {
@@ -175,7 +184,25 @@ func abs(x int) int {
175184
### **TypeScript**
176185

177186
```ts
178-
187+
function minimumAverageDifference(nums: number[]): number {
188+
const n = nums.length;
189+
let pre = 0;
190+
let suf = nums.reduce((a, b) => a + b);
191+
let ans = 0;
192+
let mi = suf;
193+
for (let i = 0; i < n; ++i) {
194+
pre += nums[i];
195+
suf -= nums[i];
196+
const a = Math.floor(pre / (i + 1));
197+
const b = n - i - 1 === 0 ? 0 : Math.floor(suf / (n - i - 1));
198+
const t = Math.abs(a - b);
199+
if (t < mi) {
200+
ans = i;
201+
mi = t;
202+
}
203+
}
204+
return ans;
205+
}
179206
```
180207

181208
### **...**

0 commit comments

Comments
 (0)