[LeetCode] Find Words Containing Character

2942. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> res;
for(int i = 0; i < words.size(); i++) {
bool ok = false;
for(auto& ch : words[i]) {
if(ch == x) ok = true;
}
if(ok) res.push_back(i);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/11/26/PS/LeetCode/find-words-containing-character/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.