Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
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 3:
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.
Constraints:
1 <= words.length <= 501 <= words[i].length <= 50x is a lowercase English letter.words[i] consists only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["leet","code"] "e"
["abc","bcd","aaaa","cbc"] "a"
["abc","bcd","aaaa","cbc"] "z"
find-target-indices-after-sorting-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2942: Find Words Containing Character
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;
}
}
// Accepted solution for LeetCode #2942: Find Words Containing Character
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
}
# Accepted solution for LeetCode #2942: Find Words Containing Character
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
return [i for i, w in enumerate(words) if x in w]
// Accepted solution for LeetCode #2942: Find Words Containing Character
impl Solution {
pub fn find_words_containing(words: Vec<String>, x: char) -> Vec<i32> {
words
.into_iter()
.enumerate()
.filter_map(|(i, w)| w.contains(x).then(|| i as i32))
.collect()
}
}
// Accepted solution for LeetCode #2942: Find Words Containing Character
function findWordsContaining(words: string[], x: string): number[] {
return words.flatMap((w, i) => (w.includes(x) ? [i] : []));
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.