Find Words Containing Character - Problem

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: The returned array may be in any order.

Input & Output

Example 1 — Basic Case
$ Input: words = ["leet","code","leetcode"], x = "e"
Output: [0,1,2]
💡 Note: All words contain 'e': "leet" has 'e' at positions 1,2, "code" has 'e' at position 3, "leetcode" has 'e' at positions 1,2,5,6
Example 2 — Some Words Match
$ Input: words = ["abc","bcd","aaaa","cbc"], x = "a"
Output: [0,2]
💡 Note: Only "abc" (index 0) and "aaaa" (index 2) contain the character 'a'
Example 3 — No Matches
$ Input: words = ["abc","bcd","def"], x = "z"
Output: []
💡 Note: None of the words contain the character 'z', so return empty array

Constraints

  • 1 ≤ words.length ≤ 50
  • 1 ≤ words[i].length ≤ 50
  • x is a lowercase English letter
  • words[i] consists only of lowercase English letters

Visualization

Tap to expand
Find Words Containing CharacterInput Array:"leet""code""leetcode"index 0index 1index 2Target: "e"Contains "e" ✓Contains "e" ✓Contains "e" ✓Output Array:012Result: [0, 1, 2]
Understanding the Visualization
1
Input
Array of words and target character
2
Process
Check each word for target character
3
Output
Array of indices where character was found
Key Takeaway
🎯 Key Insight: We must examine every word since any could contain the target character
Asked in
Google 25 Amazon 20 Microsoft 15
21.1K Views
Medium Frequency
~10 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen