-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
49 lines (42 loc) · 1.1 KB
/
Copy pathsolution.cpp
File metadata and controls
49 lines (42 loc) · 1.1 KB
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
47
48
49
class Solution
{
public:
string minWindow(string &s1, string &s2)
{
int n = s1.size(), m = s2.size();
int minLen = INT_MAX;
int start = -1;
for (int i = 0; i < n; i++)
{
if (s1[i] != s2[0])
continue;
int p1 = i, p2 = 0;
// Forward scan to match s2
while (p1 < n && p2 < m)
{
if (s1[p1] == s2[p2])
p2++;
p1++;
}
if (p2 == m)
{
int end = p1 - 1;
// Backward shrink
p2 = m - 1;
while (p2 >= 0)
{
if (s1[end] == s2[p2])
p2--;
end--;
}
end++;
if (p1 - end < minLen)
{
minLen = p1 - end;
start = end;
}
}
}
return start == -1 ? "" : s1.substr(start, minLen);
}
};