Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec465a3

Browse files
feat: add solutions to lc problem: No.2942 (doocs#2020)
1 parent 9f4fad2 commit ec465a3

File tree

7 files changed

+162
-6
lines changed

7 files changed

+162
-6
lines changed

‎solution/2900-2999/2942.Find Words Containing Character/README.md‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,89 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:遍历**
57+
58+
我们直接遍历字符串数组 $words$ 中的每一个字符串 $words[i],ドル如果 $x$ 在 $words[i]$ 中出现,就将 $i$ 加入答案数组中。
59+
60+
遍历结束后,返回答案数组即可。
61+
62+
时间复杂度 $O(L),ドル其中 $L$ 为字符串数组 $words$ 中所有字符串的长度和。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

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

6270
```python
63-
71+
class Solution:
72+
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
73+
return [i for i, w in enumerate(words) if x in w]
6474
```
6575

6676
### **Java**
6777

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

7080
```java
71-
81+
class Solution {
82+
public List<Integer> findWordsContaining(String[] words, char x) {
83+
List<Integer> ans = new ArrayList<>();
84+
for (int i = 0; i < words.length; ++i) {
85+
if (words[i].indexOf(x) != -1) {
86+
ans.add(i);
87+
}
88+
}
89+
return ans;
90+
}
91+
}
7292
```
7393

7494
### **C++**
7595

7696
```cpp
77-
97+
class Solution {
98+
public:
99+
vector<int> findWordsContaining(vector<string>& words, char x) {
100+
vector<int> ans;
101+
for (int i = 0; i < words.size(); ++i) {
102+
if (words[i].find(x) != string::npos) {
103+
ans.push_back(i);
104+
}
105+
}
106+
return ans;
107+
}
108+
};
78109
```
79110
80111
### **Go**
81112
82113
```go
114+
func findWordsContaining(words []string, x byte) (ans []int) {
115+
for i, w := range words {
116+
for _, c := range w {
117+
if byte(c) == x {
118+
ans = append(ans, i)
119+
break
120+
}
121+
}
122+
}
123+
return
124+
}
125+
```
83126

127+
### **TypeScript**
128+
129+
```ts
130+
function findWordsContaining(words: string[], x: string): number[] {
131+
const ans: number[] = [];
132+
for (let i = 0; i < words.length; ++i) {
133+
if (words[i].includes(x)) {
134+
ans.push(i);
135+
}
136+
}
137+
return ans;
138+
}
84139
```
85140

86141
### **...**

‎solution/2900-2999/2942.Find Words Containing Character/README_EN.md‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,85 @@
4747

4848
## Solutions
4949

50+
**Solution 1: Traversal**
51+
52+
We directly traverse each string `words[i]` in the string array `words`. If `x` appears in `words[i]`, we add `i` to the answer array.
53+
54+
After the traversal, we return the answer array.
55+
56+
The time complexity is $O(L),ドル where $L$ is the sum of the lengths of all strings in the array `words`. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
57+
5058
<!-- tabs:start -->
5159

5260
### **Python3**
5361

5462
```python
55-
63+
class Solution:
64+
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
65+
return [i for i, w in enumerate(words) if x in w]
5666
```
5767

5868
### **Java**
5969

6070
```java
61-
71+
class Solution {
72+
public List<Integer> findWordsContaining(String[] words, char x) {
73+
List<Integer> ans = new ArrayList<>();
74+
for (int i = 0; i < words.length; ++i) {
75+
if (words[i].indexOf(x) != -1) {
76+
ans.add(i);
77+
}
78+
}
79+
return ans;
80+
}
81+
}
6282
```
6383

6484
### **C++**
6585

6686
```cpp
67-
87+
class Solution {
88+
public:
89+
vector<int> findWordsContaining(vector<string>& words, char x) {
90+
vector<int> ans;
91+
for (int i = 0; i < words.size(); ++i) {
92+
if (words[i].find(x) != string::npos) {
93+
ans.push_back(i);
94+
}
95+
}
96+
return ans;
97+
}
98+
};
6899
```
69100
70101
### **Go**
71102
72103
```go
104+
func findWordsContaining(words []string, x byte) (ans []int) {
105+
for i, w := range words {
106+
for _, c := range w {
107+
if byte(c) == x {
108+
ans = append(ans, i)
109+
break
110+
}
111+
}
112+
}
113+
return
114+
}
115+
```
73116

117+
### **TypeScript**
118+
119+
```ts
120+
function findWordsContaining(words: string[], x: string): number[] {
121+
const ans: number[] = [];
122+
for (let i = 0; i < words.length; ++i) {
123+
if (words[i].includes(x)) {
124+
ans.push(i);
125+
}
126+
}
127+
return ans;
128+
}
74129
```
75130

76131
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
vector<int> findWordsContaining(vector<string>& words, char x) {
4+
vector<int> ans;
5+
for (int i = 0; i < words.size(); ++i) {
6+
if (words[i].find(x) != string::npos) {
7+
ans.push_back(i);
8+
}
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func findWordsContaining(words []string, x byte) (ans []int) {
2+
for i, w := range words {
3+
for _, c := range w {
4+
if byte(c) == x {
5+
ans = append(ans, i)
6+
break
7+
}
8+
}
9+
}
10+
return
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public List<Integer> findWordsContaining(String[] words, char x) {
3+
List<Integer> ans = new ArrayList<>();
4+
for (int i = 0; i < words.length; ++i) {
5+
if (words[i].indexOf(x) != -1) {
6+
ans.add(i);
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 findWordsContaining(self, words: List[str], x: str) -> List[int]:
3+
return [i for i, w in enumerate(words) if x in w]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function findWordsContaining(words: string[], x: string): number[] {
2+
const ans: number[] = [];
3+
for (let i = 0; i < words.length; ++i) {
4+
if (words[i].includes(x)) {
5+
ans.push(i);
6+
}
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /