LeetCode 2942: Find Words Containing Character (Linear Scan + Index Collection)
LeetCode 2942StringArrayToday we solve LeetCode 2942 - Find Words Containing Character.
Source: https://leetcode.com/problems/find-words-containing-character/
English
Problem Summary
Given an array words and a character x, return all indices i such that words[i] contains x.
Key Idea
This is a direct filtering task. Scan words from left to right, test if each word contains x, and append its index when true.
Algorithm
- Initialize an empty result list.
- For each index
iinwords, checkwords[i]for characterx. - If found, push
iinto result. - Return result.
Complexity
- Time: O(total characters across all words)
- Space: O(k) for output indices
Code (Java / Go / C++ / Python / JavaScript)
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;
}
}func findWordsContaining(words []string, x byte) []int {
ans := make([]int, 0)
for i, w := range words {
for j := 0; j < len(w); j++ {
if w[j] == x {
ans = append(ans, i)
break
}
}
}
return ans
}class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> ans;
for (int i = 0; i < (int)words.size(); i++) {
if (words[i].find(x) != string::npos) {
ans.push_back(i);
}
}
return ans;
}
};class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
ans = []
for i, w in enumerate(words):
if x in w:
ans.append(i)
return ans/**
* @param {string[]} words
* @param {character} x
* @return {number[]}
*/
var findWordsContaining = function(words, x) {
const ans = [];
for (let i = 0; i < words.length; i++) {
if (words[i].includes(x)) {
ans.push(i);
}
}
return ans;
};中文
题目概述
给你字符串数组 words 和字符 x,返回所有满足 words[i] 包含 x 的下标 i。
核心思路
这是一个线性筛选问题:按顺序遍历每个单词,判断是否包含 x,包含就记录下标。
算法步骤
- 准备结果数组
ans。 - 遍历
words的每个位置i。 - 若
words[i]中出现x,将i加入ans。 - 遍历结束后返回
ans。
复杂度分析
- 时间复杂度:O(所有字符串总长度)
- 空间复杂度:O(k),k 为结果下标个数
代码(Java / Go / C++ / Python / JavaScript)
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;
}
}func findWordsContaining(words []string, x byte) []int {
ans := make([]int, 0)
for i, w := range words {
for j := 0; j < len(w); j++ {
if w[j] == x {
ans = append(ans, i)
break
}
}
}
return ans
}class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> ans;
for (int i = 0; i < (int)words.size(); i++) {
if (words[i].find(x) != string::npos) {
ans.push_back(i);
}
}
return ans;
}
};class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
ans = []
for i, w in enumerate(words):
if x in w:
ans.append(i)
return ans/**
* @param {string[]} words
* @param {character} x
* @return {number[]}
*/
var findWordsContaining = function(words, x) {
const ans = [];
for (let i = 0; i < words.length; i++) {
if (words[i].includes(x)) {
ans.push(i);
}
}
return ans;
};
Comments