String Matching in an Array Problem & Solution

Given an array of string words. Return all strings in words which is substring of another word in any order.

String words[i] is substring of words[j], if can be obtained removing some characters to left and/or right side of words[j].

See the string matching in an array problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")

static const int _=[](){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
  vector<string> stringMatching(vector<string>& words) {
    set<string> result;

    for (int i = 0; i < words.size(); ++i) {
      for (int j = 0; j < words.size(); ++j) {
        if (i != j && words[i].find(words[j]) != string::npos) {

    return vector<string>(result.begin(), result.end());

Start Here

Many paths, there are. Follow yours, you must.