-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHwInterview.h
46 lines (44 loc) · 1.21 KB
/
HwInterview.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Created by Hywfred on 2020/3/22.
//
#ifndef LEETCODEINCPP_HWINTERVIEW_H
#define LEETCODEINCPP_HWINTERVIEW_H
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<int> findSubstrings(string s, vector<string> &words) {
vector<int> res;
int len = words.size();
if (!len) {
return res;
}
int l = words[0].size();
// 先对原始单词组排序
sort(words.begin(), words.end());
int sl = s.size();
for (int i = 0; i < sl; ++i) {
vector<string> tmp;
// 从 i 开始取相同个数个单词
if (i + len * l <= sl) {
for (int j = 0; j < len; ++j) {
string str = s.substr(i + j*l, l);
tmp.push_back(str);
}
// 对取出的新单词组进行排序
sort(tmp.begin(), tmp.end());
// 比较排序后的单词是否都一样
bool same = true;
for (int j = 0; j < len; ++j) {
if (tmp[j] != words[j]) {
same = false;
}
}
if (same) {
res.push_back(i);
}
}
}
return res;
}
#endif //LEETCODEINCPP_HWINTERVIEW_H