2942. Find Words Containing Character LeetCode Solution
In this guide, you will get 2942. Find Words Containing Character LeetCode Solution with the best time and space complexity. The solution to Find Words Containing Character problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Words Containing Character solution in C++
- Find Words Containing Character solution in Java
- Find Words Containing Character solution in Python
- Additional Resources
Problem Statement of Find Words Containing Character
You are given a 0-indexed array of strings words and a character x.
Return an array of indices representing the words that contain the character x.
Note that the returned array may be in any order.
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.
Complexity Analysis
- Time Complexity: O(\Sigma |\texttt{words[i]}|)
- Space Complexity: O(n)
2942. Find Words Containing Character LeetCode Solution in C++
class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> ans;
for (int i = 0; i < words.size(); ++i)
if (words[i].find(x) != string::npos)
ans.push_back(i);
return ans;
}
};
/* code provided by PROGIEZ */
2942. Find Words Containing Character LeetCode Solution in Java
N/A
// code provided by PROGIEZ
2942. Find Words Containing Character LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.