# 【LeetCode】 1408. String Matching in an Array
## Description
> 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].
> Constraints:
> * 1 <= words.length <= 100
> * 1 <= words[i].length <= 30
> * words[i] contains only lowercase English letters.
> * It's guaranteed that words[i] will be unique.
> 給一個字串陣列words。以任何排序回傳所有在words中,是其他任意字串的子字串的字串。
> 如果words[i]在右邊或左邊加入任意字元後可以變成words[j],字串words[i]是words[j]的子字串。
> 限制:
> * 1 <= words.length <= 100
> * 1 <= words[i].length <= 30
> * words[i] 只包含小寫英文字母。
> * 保證words[i]的獨特性(不會重複)。
## Example:
```
Example 1:
Input: words = ["mass","as","hero","superhero"]
Output: ["as","hero"]
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
["hero","as"] is also a valid answer.
Example 2:
Input: words = ["leetcode","et","code"]
Output: ["et","code"]
Explanation: "et", "code" are substring of "leetcode".
Example 3:
Input: words = ["blue","green","bu"]
Output: []
```
## Solution
* C++中提供了`find()`的功能,可以直接使用。
* 如果有找到就代表參數是物件的子字串。
* 為了避免重複,使用`unique()`將最後答案移除重複的元素。
### Code
```C++=1
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> ans;
for(int i = 0; i < words.size(); i++)
{
for(int j = 0; j < words.size(); j++)
{
if(i == j) continue;
if(words[i].find(words[j]) != std::string::npos)
ans.push_back(words[j]);
}
}
sort( ans.begin(), ans.end() );
ans.erase( unique( ans.begin(), ans.end() ), ans.end() );
return ans;
}
};
```
###### tags: `LeetCode` `C++`