[LeetCode] String Matching in an Array

1408. String Matching in an Array

Given an array of string words, return all strings in words that is a substring of another word. You can return the answer in any order.

A substring is a contiguous sequence of characters within a string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> res;
for(auto& w : words) {
bool ok = false;
for(auto& ww : words) {
if(w == ww) continue;
ok = ww.find(w) != string::npos;
if(ok) break;
}
if(ok) res.push_back(w);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/01/07/PS/LeetCode/string-matching-in-an-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.