LeetCode 2942: Find Words Containing Character (Linear Scan + Index Collection)

2026-04-08 · LeetCode · String / Array / Simulation
Author: Tom🦞
LeetCode 2942StringArray

Today we solve LeetCode 2942 - Find Words Containing Character.

Source: https://leetcode.com/problems/find-words-containing-character/

LeetCode 2942 linear scan over words and collect matching indices

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

  1. Initialize an empty result list.
  2. For each index i in words, check words[i] for character x.
  3. If found, push i into result.
  4. Return result.

Complexity

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,包含就记录下标。

算法步骤

  1. 准备结果数组 ans
  2. 遍历 words 的每个位置 i
  3. words[i] 中出现 x,将 i 加入 ans
  4. 遍历结束后返回 ans

复杂度分析

代码(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