Return Indices of Words Containing a Given Character
Quick Overview
Return every word index whose string contains the target lowercase character, including separate positions with identical words and allowing any output order.
Return Indices of Words Containing a Given Character
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
# Return Indices of Words Containing a Given Character
You are given a 0-indexed array of strings `words` and a character `x`.
Return an array containing the indices of all words that contain `x`. Each qualifying index should appear once. The returned indices may be in any order.
### Examples
**Example 1**
```text
Input: words = ["map","mammal","sky","map"], x = "m"
Output: [0,1,3]
```
Indices `0`, `1`, and `3` contain `'m'`. The repeated word `"map"` occurs at two different indices, so both indices qualify. `"mammal"` contributes its index only once despite having several occurrences of `'m'`. Any ordering of these three indices is valid.
**Example 2**
```text
Input: words = ["stone","river"], x = "z"
Output: []
```
Neither word contains `'z'`, so return an empty array.
### Constraints
- `1 <= words.length <= 50`.
- `1 <= words[i].length <= 50`.
- `x` is a lowercase English letter.
- Each `words[i]` contains only lowercase English letters.
### Clarifying Questions
- Should we return words or indices? Return their 0-based indices.
- If a word contains the character multiple times, should its index repeat? No; the index represents one qualifying word occurrence.
- Can identical words occur at different positions? No uniqueness guarantee is given; inspect each position independently.
- Must indices be sorted? No; any order is allowed.
- What happens when there are no matches? Return an empty array.
### C++ Interface
```cpp
class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
}
};
```
### Reasoning Checkpoints
- Distinguish repeated characters within one word from repeated words at different indices.
- Include every matching index and exclude every nonmatching index.
- Respect the any-order result contract and account for the output size.
```hint Stop after the first match within a word
Inspect each word's characters. Once the target character is found, record that word's index and move to the next word.
```
Overview: Return every word index whose string contains the target lowercase character, including separate positions with identical words and allowing any output order.
Return Indices of Words Containing a Given Character
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0
Return Indices of Words Containing a Given Character
You are given a 0-indexed array of strings words and a character x.
Return an array containing the indices of all words that contain x. Each qualifying index should appear once. The returned indices may be in any order.
Examples
Example 1
Input: words = ["map","mammal","sky","map"], x = "m"
Output: [0,1,3]
Indices 0, 1, and 3 contain 'm'. The repeated word "map" occurs at two different indices, so both indices qualify. "mammal" contributes its index only once despite having several occurrences of 'm'. Any ordering of these three indices is valid.
Example 2
Input: words = ["stone","river"], x = "z"
Output: []
Neither word contains 'z', so return an empty array.
Constraints
1 <= words.length <= 50
.
1 <= words[i].length <= 50
.
x
is a lowercase English letter.
Each
words[i]
contains only lowercase English letters.
Clarifying Questions Guidance
Should we return words or indices? Return their 0-based indices.
If a word contains the character multiple times, should its index repeat? No; the index represents one qualifying word occurrence.
Can identical words occur at different positions? No uniqueness guarantee is given; inspect each position independently.
Must indices be sorted? No; any order is allowed.
What happens when there are no matches? Return an empty array.