Sitemap
Photo by Chris Ried on Unsplash

LeetCode day319–2942

1 min readMay 24, 2025

--

Everyday has one random leetcode quiz. Today’s quiz is 2942. Find Words Containing Character

Description:

You are given a 0-indexed array of strings words and a character x.

Return an array of indices representing the words that contain the character x.

Note that the returned array may be in any order.

Get Jimmy’s stories in your inbox

Join Medium for free to get updates from this writer.

Example 1:

Input: words = ["leet","code"], x = "e"
Output: [0,1]
Explanation: "e" occurs in both words: "leet", and "code". Hence, we return indices 0 and 1.

Example 2:

Input: words = ["abc","bcd","aaaa","cbc"], x = "a"
Output: [0,2]
Explanation: "a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.

Example 2:

Input: words = ["abc","bcd","aaaa","cbc"], x = "z"
Output: []
Explanation: "z" does not occur in any of the words. Hence, we return an empty array.

Check each word in words , if it contains x , insert its index into ans .

At the end, return ans as answer.

If words length is n, each word in words is m, the time complexity is O(n*m)

/**
* @param {string[]} words
* @param {character} x
* @return {number[]}
*/
var findWordsContaining = function (words, x) {
const ans = []
words.forEach((word, i) => {
if (word.includes(x)) {
ans.push(i)
}
})
return ans
};

--

--

Jimmy
Jimmy

Written by Jimmy

The frontend enginer is learning