「 每日一练,快乐水题 」1408. 数组中的字符串匹配
原创
©著作权归作者所有:来自51CTO博客作者wx63dcc0114075e的原创作品,请联系作者获取转载授权,否则将追究法律责任
文章目录
- 🔴力扣原题:
- 🟠题目简述:
- 🟡解题思路:
- 🟢C++代码:
- 🔵结果展示:
🔴力扣原题:
1408. 数组中的字符串匹配
🟠题目简述:
给你一个字符串数组 words ,数组中的每个字符串都可以看作是一个单词。请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词。
如果你可以删除 words[j] 最左侧和/或最右侧的若干字符得到 word[i] ,那么字符串 words[i] 就是 words[j] 的一个子字符串。
🟡解题思路:
- 按照题意暴力遍历;
- 然后利用
find
函数查子串; - 然后去重(官方的去重比较好,我这个比较沙雕);
- over;
🟢C++代码:
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> vec;
for(auto word:words)
{
for(auto word1:words)
{
if(word1.find(word) != word1.npos)
{
///< 去重
auto it = find(vec.begin(),vec.end(),word);
if(it == vec.end() && (word1 != word))
{
vec.push_back(word);
}
// cout <<"word1:" << word1 << " word:" << word << endl;
}
}
}
return vec;
}
};
🔵结果展示: