Leetcode 2942. Find Words Containing Character
You are asked to find all the words in a given list that contain a specific character.
import java.util.ArrayList;
import java.util.List;
public class FindWordsContainingCharacter {
public static List<String> findWordsContainingChar(List<String> words, char ch) {
List<String> result = new ArrayList<>();
for (String word : words) {
if (word.indexOf(ch) >= 0) {
result.add(word);
}
}
return result;
}
public static void main(String[] args) {
// Test cases
List<String> words = List.of("hello", "world", "leetcode", "java", "python");
char ch = 'o';
List<String> result = findWordsContainingChar(words, ch);
System.out.println(result); // Output: [hello, world]
}
}
This solution appropriately handles the given problem by iterating through the list of words and checking for the presence of the character in each word.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?