Find Words Containing Character

Table of contents

In this question, we need to check each element of the given array in the search of a given character or text.

What you need to know:

For this question :

  • You need to be able to iterate through a string array

  • You need to know how to use exampleString.contains() method

  • How to convert a char into a string?

  • Use ArrayList to hold the given words

Solution:

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> findWordsContaining(List<String> words, char x) {
        List<Integer> ans = new ArrayList<>();
        int n = words.size();
        for (int i = 0; i < n; i++) {
            for (char j : words.get(i).toCharArray()) {
                if (j == x) {
                    ans.add(i);
                    break;
                }
            }
        }
        return ans;
    }
}