-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin1.cpp
More file actions
46 lines (43 loc) · 1.02 KB
/
win1.cpp
File metadata and controls
46 lines (43 loc) · 1.02 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
string smallestWindow (string s, string p)
{
int dp[256]={0};
int n=s.size();
int m=p.size();
int cnt=0;
for(int i=0;i<m;i++)
{
if(dp[p[i]]==0)
{
cnt++;
}
dp[p[i]]++;
}
int i=0; int j=0; int ans=INT_MAX;int start;
while(j<n)
{
dp[s[j]]--;
if(dp[s[j]]==0)
cnt--;
if(cnt==0)
{
while(cnt==0)
{
if(j-i+1<ans)
{
ans=j-i+1; start=i;
}
dp[s[i]]++;
if(dp[s[i]]>0)
{
cnt++;
}
i++;
}
}
j++;
}
if(ans==INT_MAX)
return "-1";
else
return s.substr(start,ans);
}