# Code language: Python classSolution: defstringMatching(self, words: List[str]) -> List[str]: return [s for s in words ifany(s in w and s != w for w in words)]
1 2 3 4
# Code language: Python classSolution: defstringMatching(self, words: List[str]) -> List[str]: returnlist(filter((lambda s: any(s != w and s in w for w in words)), words))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Code language: Java classSolution { public List<String> stringMatching(String[] words) { List<String> ans = newArrayList<>(); for (inti=0, n = words.length; i < n; ++i) { Strings= words[i]; for (intj=0; j < n; ++j) { if (i == j) continue; if (words[j].indexOf(s) >= 0) { ans.add(s); break; } } } return ans; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Code language: C++ classSolution { public: vector<string> stringMatching(vector<string>& words){ vector<string> ans; for (int i = 0, n = words.size(); i < n; ++i) { string& s = words[i]; for (int j = 0; j < n; ++j) { if (i == j) continue; if (words[j].find(s) != string::npos) { ans.emplace_back(s); break; } } } return ans; } };