-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3302.cpp
More file actions
33 lines (33 loc) · 868 Bytes
/
Copy path3302.cpp
File metadata and controls
33 lines (33 loc) · 868 Bytes
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
class Solution {
public:
vector<int> validSequence(string word1, string word2) {
int m = word1.size();
int n = word2.size();
vector<int> suffix(m + 1, 0);
int cnt = 0;
int j = n - 1;
for (int i = m - 1; i >= 0; --i) {
if (j >= 0 && word1[i] == word2[j]) {
cnt++;
j--;
}
suffix[i] = cnt;
}
vector<int> res;
bool flag = false;
j = 0;
for (int i = 0; i < m && j < n; ++i) {
if (word1[i] == word2[j]) {
res.push_back(i);
j++;
}
else if (!flag && suffix[i + 1] >= n - j - 1) {
flag = true;
res.push_back(i);
j++;
}
}
if (j != n) return {};
return res;
}
};