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

feat: add solutions to lc problem: No.2942 #2020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 1 commit into main from dev
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions solution/2900-2999/2942.Find Words Containing Character/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,89 @@

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

**方法一:遍历**

我们直接遍历字符串数组 $words$ 中的每一个字符串 $words[i],ドル如果 $x$ 在 $words[i]$ 中出现,就将 $i$ 加入答案数组中。

遍历结束后,返回答案数组即可。

时间复杂度 $O(L),ドル其中 $L$ 为字符串数组 $words$ 中所有字符串的长度和。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
return [i for i, w in enumerate(words) if x in w]
```

### **Java**

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

```java

class Solution {
public List<Integer> findWordsContaining(String[] words, char x) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < words.length; ++i) {
if (words[i].indexOf(x) != -1) {
ans.add(i);
}
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> ans;
for (int i = 0; i < words.size(); ++i) {
if (words[i].find(x) != string::npos) {
ans.push_back(i);
}
}
return ans;
}
};
```

### **Go**

```go
func findWordsContaining(words []string, x byte) (ans []int) {
for i, w := range words {
for _, c := range w {
if byte(c) == x {
ans = append(ans, i)
break
}
}
}
return
}
```

### **TypeScript**

```ts
function findWordsContaining(words: string[], x: string): number[] {
const ans: number[] = [];
for (let i = 0; i < words.length; ++i) {
if (words[i].includes(x)) {
ans.push(i);
}
}
return ans;
}
```

### **...**
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,85 @@

## Solutions

**Solution 1: Traversal**

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.

After the traversal, we return the answer array.

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)$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
return [i for i, w in enumerate(words) if x in w]
```

### **Java**

```java

class Solution {
public List<Integer> findWordsContaining(String[] words, char x) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < words.length; ++i) {
if (words[i].indexOf(x) != -1) {
ans.add(i);
}
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> ans;
for (int i = 0; i < words.size(); ++i) {
if (words[i].find(x) != string::npos) {
ans.push_back(i);
}
}
return ans;
}
};
```

### **Go**

```go
func findWordsContaining(words []string, x byte) (ans []int) {
for i, w := range words {
for _, c := range w {
if byte(c) == x {
ans = append(ans, i)
break
}
}
}
return
}
```

### **TypeScript**

```ts
function findWordsContaining(words: string[], x: string): number[] {
const ans: number[] = [];
for (let i = 0; i < words.length; ++i) {
if (words[i].includes(x)) {
ans.push(i);
}
}
return ans;
}
```

### **...**
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> ans;
for (int i = 0; i < words.size(); ++i) {
if (words[i].find(x) != string::npos) {
ans.push_back(i);
}
}
return ans;
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func findWordsContaining(words []string, x byte) (ans []int) {
for i, w := range words {
for _, c := range w {
if byte(c) == x {
ans = append(ans, i)
break
}
}
}
return
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public List<Integer> findWordsContaining(String[] words, char x) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < words.length; ++i) {
if (words[i].indexOf(x) != -1) {
ans.add(i);
}
}
return ans;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
return [i for i, w in enumerate(words) if x in w]
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function findWordsContaining(words: string[], x: string): number[] {
const ans: number[] = [];
for (let i = 0; i < words.length; ++i) {
if (words[i].includes(x)) {
ans.push(i);
}
}
return ans;
}

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