Find Words Containing Character

Difficulty: Easy

Topics: Array, String

Introduction

In this tutorial, we will learn how to find the indices of words in a list that contain a specific character. This is a common problem in programming that helps us understand how to work with arrays and strings effectively. We will implement a solution in PHP, but the concepts can be applied in any programming language.

Prerequisites

  • Basic understanding of arrays and strings in programming.
  • Familiarity with PHP syntax.
  • Knowledge of functions and loops in programming.

Problem Statement

You are given a 0-indexed array of strings words and a character x. Your task is to return an array of indices representing the words that contain the character x.

Note: The returned array may be in any order.

Examples

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 <= 50
  • 1 <= words[i].length <= 50
  • x is a lowercase English letter.
  • words[i] consists only of lowercase English letters.

Approach

To solve this problem, we will follow these steps:

  1. Initialize an empty array to store the indices of words that contain the specified character.
  2. Iterate through each word in the input list while keeping track of the current index.
  3. Check if the character exists in the current word using the strpos function. If the character is found, add the current index to the result array.
  4. Return the result array after processing all words.

Implementation

Let’s implement this solution in PHP:

<?php
/**
 * @param String[] $words
 * @param String $x
 * @return Integer[]
 */
function findWordsContaining($words, $x) {
    $result = array();
    foreach ($words as $index => $word) {
        if (strpos($word, $x) !== false) {
            $result[] = $index;
        }
    }
    return $result;
}

// Example 1:
$words1 = array("leet", "code");
$x1 = "e";
print_r(findWordsContaining($words1, $x1)); // Output: [0,1]

// Example 2:
$words2 = array("abc", "bcd", "aaaa", "cbc");
$x2 = "a";
print_r(findWordsContaining($words2, $x2)); // Output: [0,2]

// Example 3:
$words3 = array("abc", "bcd", "aaaa", "cbc");
$x3 = "z";
print_r(findWordsContaining($words3, $x3)); // Output: []
?>

Explanation

  • Initialization: We start by creating an empty array $result to hold the indices of words containing the character.
  • Iteration: Using a loop, we go through each word in the input array. For each word, we check if the character x is present using strpos, which returns the position of the first occurrence of x or false if not found.
  • Check and Collect Indices: If strpos returns a valid position (i.e., not false), we add the current index to $result.
  • Return Result: Finally, after processing all words, we return the array of indices.

This solution efficiently handles the constraints and ensures that we correctly identify all words containing the specified character, returning their indices in any order.

Conclusion

In this tutorial, we learned how to find the indices of words containing a specific character in a list using PHP. We explored the problem statement, constraints, and implemented a solution step-by-step. This exercise not only enhances our understanding of arrays and strings but also improves our problem-solving skills in programming.

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Sources:

Source: Original Article