Find Words Containing Character — Problem & Solution
Find Words Containing Character solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Find Words Containing Character is an easy coding problem on arrays and strings, asked in Amazon, Google and Capgemini interviews.
- Difficulty: Easy
- Topics: Arrays, Strings
- Asked at: Amazon, Google, Capgemini
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an array of strings words and a single character x, return the indices of the words that contain x, in increasing order.
Example 1
Input: words = ["code","kairo","judge"], x = "o"
Output: [0,1]
Explanation: `judge` has no `o`.
Example 2
Input: words = ["abc","bcd","aaaa","cbc"], x = "a"
Output: [0,2]
Example 3
Input: words = ["abc","bcd","aaaa","cbc"], x = "z"
Output: []
Explanation: No word contains `z`.
Constraints
1 <= words.length <= 501 <= words[i].length <= 50x is a lowercase English letter.words[i] consists only of lowercase English letters.
Hints for Find Words Containing Character
Hint 1
Walk the array in order and test each word for the character.
Hint 2
Collecting indices left to right already gives increasing order.
Hint 3
An empty result is valid — return an empty array, not null.
How to solve Find Words Containing Character
A single pass: for each index, if the word contains x, record the index.
Approach
- Iterate over
wordswith its index. - Test membership of
xwith a substring search or a character loop. - Append the index to the result when found.
Why it works
Scanning in index order is what makes the output sorted for free — no sort is needed. The only thing to be careful about is returning an empty array rather than null when nothing matches, which some judges treat differently.
Complexity
- Time —
O(n · m) - Space —
O(n) for the output
Pitfalls
- Return indices, not the words themselves.
- An empty answer must still be an array.
- A word containing
xseveral times is still recorded once.
Reference solution
Python
from typing import List
def findWordsContaining(words: List[str], x: str) -> List[int]:
return [i for i, w in enumerate(words) if x in w]Java
public static int[] findWordsContaining(String[] words, String x) {
List<Integer> out = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
if (words[i].indexOf(x) >= 0) out.add(i);
}
int[] res = new int[out.size()];
for (int i = 0; i < res.length; i++) res[i] = out.get(i);
return res;
}C++
vector<int> findWordsContaining(vector<string>& words, string x) {
vector<int> out;
for (int i = 0; i < (int) words.size(); i++) {
if (words[i].find(x) != string::npos) out.push_back(i);
}
return out;
}JavaScript
var findWordsContaining = function(words, x) {
var out = [];
for (var i = 0; i < words.length; i++) {
if (words[i].indexOf(x) !== -1) out.push(i);
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.