Skip to content

Commit 40384d4

Browse files
committed
feat: add solutions to lc problem: No.2185
No.2185.Counting Words With a Given Prefix
1 parent 9efd871 commit 40384d4

File tree

9 files changed

+125
-10
lines changed

9 files changed

+125
-10
lines changed

solution/0000-0099/0033.Search in Rotated Sorted Array/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ class Solution:
8383
while left < right:
8484
mid = (left + right) >> 1
8585
if nums[0] <= nums[mid]:
86-
if nums[0] <= target and target <= nums[mid]:
86+
if nums[0] <= target <= nums[mid]:
8787
right = mid
8888
else:
8989
left = mid + 1
9090
else:
91-
if nums[mid] < target and target <= nums[n - 1]:
91+
if nums[mid] < target <= nums[n - 1]:
9292
left = mid + 1
9393
else:
9494
right = mid

solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ class Solution:
5050
while left < right:
5151
mid = (left + right) >> 1
5252
if nums[0] <= nums[mid]:
53-
if nums[0] <= target and target <= nums[mid]:
53+
if nums[0] <= target <= nums[mid]:
5454
right = mid
5555
else:
5656
left = mid + 1
5757
else:
58-
if nums[mid] < target and target <= nums[n - 1]:
58+
if nums[mid] < target <= nums[n - 1]:
5959
left = mid + 1
6060
else:
6161
right = mid

solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ def search(self, nums: List[int], target: int) -> int:
55
while left < right:
66
mid = (left + right) >> 1
77
if nums[0] <= nums[mid]:
8-
if nums[0] <= target and target <= nums[mid]:
8+
if nums[0] <= target <= nums[mid]:
99
right = mid
1010
else:
1111
left = mid + 1
1212
else:
13-
if nums[mid] < target and target <= nums[n - 1]:
13+
if nums[mid] < target <= nums[n - 1]:
1414
left = mid + 1
1515
else:
1616
right = mid

solution/2100-2199/2185.Counting Words With a Given Prefix/README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,27 @@
4949
<!-- 这里可写当前语言的特殊实现逻辑 -->
5050

5151
```python
52-
52+
class Solution:
53+
def prefixCount(self, words: List[str], pref: str) -> int:
54+
return sum(w.startswith(pref) for w in words)
5355
```
5456

5557
### **Java**
5658

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

5961
```java
60-
62+
class Solution {
63+
public int prefixCount(String[] words, String pref) {
64+
int ans = 0;
65+
for (String w : words) {
66+
if (w.startsWith(pref)) {
67+
++ans;
68+
}
69+
}
70+
return ans;
71+
}
72+
}
6173
```
6274

6375
### **TypeScript**
@@ -75,6 +87,35 @@ function prefixCount(words: string[], pref: string): number {
7587
}
7688
```
7789

90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int prefixCount(vector<string>& words, string pref) {
96+
int ans = 0;
97+
for (auto& w : words)
98+
if (w.find(pref) == 0)
99+
++ans;
100+
return ans;
101+
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func prefixCount(words []string, pref string) int {
109+
ans := 0
110+
for _, w := range words {
111+
if strings.HasPrefix(w, pref) {
112+
ans++
113+
}
114+
}
115+
return ans
116+
}
117+
```
118+
78119
### **...**
79120

80121
```

solution/2100-2199/2185.Counting Words With a Given Prefix/README_EN.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,25 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def prefixCount(self, words: List[str], pref: str) -> int:
48+
return sum(w.startswith(pref) for w in words)
4749
```
4850

4951
### **Java**
5052

5153
```java
52-
54+
class Solution {
55+
public int prefixCount(String[] words, String pref) {
56+
int ans = 0;
57+
for (String w : words) {
58+
if (w.startsWith(pref)) {
59+
++ans;
60+
}
61+
}
62+
return ans;
63+
}
64+
}
5365
```
5466

5567
### **TypeScript**
@@ -67,6 +79,35 @@ function prefixCount(words: string[], pref: string): number {
6779
}
6880
```
6981

82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int prefixCount(vector<string>& words, string pref) {
88+
int ans = 0;
89+
for (auto& w : words)
90+
if (w.find(pref) == 0)
91+
++ans;
92+
return ans;
93+
}
94+
};
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
func prefixCount(words []string, pref string) int {
101+
ans := 0
102+
for _, w := range words {
103+
if strings.HasPrefix(w, pref) {
104+
ans++
105+
}
106+
}
107+
return ans
108+
}
109+
```
110+
70111
### **...**
71112

72113
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int prefixCount(vector<string>& words, string pref) {
4+
int ans = 0;
5+
for (auto& w : words)
6+
if (w.find(pref) == 0)
7+
++ans;
8+
return ans;
9+
}
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func prefixCount(words []string, pref string) int {
2+
ans := 0
3+
for _, w := range words {
4+
if strings.HasPrefix(w, pref) {
5+
ans++
6+
}
7+
}
8+
return ans
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int prefixCount(String[] words, String pref) {
3+
int ans = 0;
4+
for (String w : words) {
5+
if (w.startsWith(pref)) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def prefixCount(self, words: List[str], pref: str) -> int:
3+
return sum(w.startswith(pref) for w in words)

0 commit comments

Comments
 (0)