LeetCode Problem Workspace

Find Words Containing Character

Identify all indices of words containing a specific character using array and string traversal techniques efficiently.

category

2

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Array plus String

bolt

Answer-first summary

Identify all indices of words containing a specific character using array and string traversal techniques efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus String

Try AiBox Copilotarrow_forward

This problem asks you to return the indices of all words in an array that contain a specified character. You can solve it by iterating through each word and checking for the presence of the character. Using nested loops over the array and string ensures you correctly identify matches and return the required indices efficiently.

Problem Statement

You are given an array of lowercase strings and a single lowercase character x. Your task is to return an array of indices representing which words contain x.

Each word in the array consists of lowercase letters only. The order of indices in the output array does not matter, and if no words contain x, return an empty array.

Examples

Example 1

Input: words = ["leet","code"], x = "e"

Output: [0,1]

"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]

"a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.

Example 3

Input: words = ["abc","bcd","aaaa","cbc"], x = "z"

Output: []

"z" does not occur in any of the words. Hence, we return an 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.

Solution Approach

Iterate Over Words

Loop through each word in the array by index. For each word, check if the target character exists inside the word. If it does, add the index to the results array.

Use Nested Loop for Character Check

For each word, iterate through its characters to see if any character matches x. This ensures you capture all occurrences even when words contain multiple characters.

Return Collected Indices

After checking all words, return the array of collected indices. The order is not important, but every word containing x must be included exactly once.

Complexity Analysis

Metric Value
Time O(n * m)
Space O(1)

The time complexity is O(n * m) where n is the number of words and m is the maximum word length because each character of every word may be checked. Space complexity is O(1) aside from the output array.

What Interviewers Usually Probe

  • Does the candidate correctly iterate through both the array and characters within each word?
  • Is the candidate returning the indices rather than the words themselves?
  • Does the solution handle cases where no words contain the target character?

Common Pitfalls or Variants

Common pitfalls

  • Returning words instead of indices.
  • Assuming the array or words are sorted and returning incorrect order.
  • Missing cases where no words contain the character, resulting in null or incorrect output.

Follow-up variants

  • Return words containing the character instead of their indices.
  • Count how many times the character occurs in each word and return those counts.
  • Find indices of words containing multiple specified characters simultaneously.

FAQ

What is the easiest way to find words containing a character in an array?

Loop through each word and check each character for a match with the target character. Collect the indices when matches are found.

Can the returned indices be in any order?

Yes, the order of indices does not matter as long as every word containing the character is included.

What if no words contain the character?

Return an empty array since no indices correspond to matches.

How does this relate to the array plus string pattern?

The pattern involves iterating over an array of strings and performing character-level checks, exactly as required here.

What are the constraints for this problem?

Array length is 1 to 50, each word length 1 to 50, all lowercase letters, and the target character is lowercase.

terminal

Solution

Solution 1: Traversal

We directly traverse each string `words[i]` in the string array `words`. If `x` appears in `words[i]`, we add `i` to the answer array.

1
2
3
class Solution:
    def findWordsContaining(self, words: List[str], x: str) -> List[int]:
        return [i for i, w in enumerate(words) if x in w]
Find Words Containing Character Solution: Array plus String | LeetCode #2942 Easy